Solved

Casting

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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. …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

757 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

21 Experts available now in Live!

Get 1:1 Help Now