Solved

Casting

Posted on 1998-09-23
4
483 Views
Last Modified: 2010-04-01
Hi,

I have an array of float data type of which I tried to change them to round numbers by casting them to unsigned char data type. But for data such as 3.99, the result of the casting is 3. What can I do so that all the data will be rounded to their nearest values?

Rgds,
MY
0
Comment
Question by:misumi
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1173586
For unsigned chars, (char)(3.99+0.5) would give the nearest value.
(If you don't insist on round-to-even for 4.5)
0
 
LVL 1

Accepted Solution

by:
Booth882 earned 10 total points
ID: 1173587
you can round it to any place you want, including the ones, with the following method:

#include <math.h>

void RoundOff ( float & TheNumber, int ThePlace)
{
// the place is the power of ten you are rounding it to, for
// example an input of -2 will round it to the hundreths place
// an input of 0 will yield the number rounded to the ones place

   float PlaceMultiple = pow ( 10, ThePlace ) ;

   TheNumber = ( ( int ) (TheNumber / PlaceMultiple + 0.5 ) )  
                          *  PlaceMultiple;
}

thats all there is to it.  it will round to the nearest of any place you specify and you dont even have to cast it as a char to do it!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1173588
I thought the idea was to have an unsigned char?

If you're not unsigned, RoundOff will fail when TheNumber is negative.
It will also fail when TheNumber is not in the range of (int)
0
 
LVL 1

Expert Comment

by:Booth882
ID: 1173589
if you really need to do it for negatives just add the following inside the method:

int Sign = 0;

if(TheNumber < 0) Sign = -1;
else Sign = 1;

and then multiply it by the 0.5 like:

TheNumber = ( ( int ) ( TheNumber / PlaceMultiple +
                         ( Sign * 0.5 ) ) ) * PlaceMultiple;

if an int doesnt give you enough room replace the ( int ) with a ( long ).  if even thats not enough you can resort to bit crunching but since you were going to cast it as a char that should be more than enough.

anything else?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now