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
Solved

Casting

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

792 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