Solved

Casting

Posted on 1998-09-23
4
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

739 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