Solved

Casting

Posted on 1998-09-23
4
481 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

912 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

23 Experts available now in Live!

Get 1:1 Help Now