Public Group

# Floating

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im trying to drop all the extra numbers after the decimal. On a float number down to just two or three. (IE 2.33 or 1.75 not 4.984298290) any help would be welcome. Im working in c btw. In dev-c++.

##### Share on other sites
To do it and continue storing the numbers I would multiply by the correct power of ten, cast to int, and then divide by the power of ten. If you are looking to display numbers with less precision this is definitely the way to go.

##### Share on other sites
But i want to keep 2 or 3 of the numbers after the decimal. Wouldnt casting to int erase my number to a whole?

##### Share on other sites
4.984298290 x 100 = 498.4298290
498.4298290 rounded off = 498
498 / 100 = 4.98

##### Share on other sites
he means...

float a = 123.45678;
int b = 0;

a += 100; // a = 12345.678
b = (int) a; // b = 12345

a = (b / 100);

Something like that.

ace

##### Share on other sites
If you casted straight to an int from a floating point you would lose all decimal places correct, however my suggestion was to first multiply the floating point number by a power of ten.

double x = 4.918563729496724;
x *= 100.0;
int temp = (int)x;
x = temp / 100.0;

edit : two whole minutes, guess I got distracted longer than I thought

##### Share on other sites
Ahh feeling of stupidy, a great way to get your butt in gear. Thanks again guys.

##### Share on other sites
Im still getting this ----> 0.89000000 after the conversion. I rember my teacher telling me about how to throw those extra 0's off but i forgot how. Im sorry im making this a pain. Thanks in advance for any help guys.

##### Share on other sites
What are you using these numbers for if, it's for printing, in c you would use
printf("%.2f",floatingpointNumber);
for 2 digits or
printf("%.3f",floatingpointNumber);
for 3 digits

The following I wrote before I released that this is in the beginners forum so it's probably irrelevant to the original poster, but should be interesting to some people at least.

If your doing math, in most cases you would would want the extra bits of accuracy. And you should be aware that certain numbers in base 10, cannot be represented perfectly in base 2. For example 0.1 is a repeating fraction in base 2.
Now for an example of what that means a float has exactly 24 binary digits of accuracy which is roughly 8 digits in base 10. but
printf("%.8f",0.1f+0.1f+0.1f+0.1f+0.1f+0.1f+0.1f+0.1f+0.1f+0.1f); //notice 10 0.1fs
returns
1.00000012
but if we use a number that can be represented in base 2 like 0.0625 (notice it's smaller than 0.1)
printf("%.8f",0.0625f+0.0625f+0.0625f+0.0625f+0.0625f+0.0625f+0.0625f+0.0625f+0.0625f+0.0625f);
returns
0.62500000
My point in writing this is that dividing by 10 or 100 wont give you exactly what you want. For example 498 / 100 is 4.98000002 (again writing it out to 8 digits). Although doing this your generally be close enough (but you should be aware of it). This is part of the reason why you don't use == with floating point numbers since 0.1 plus itself ten times != 1.0, as far as the computer is concerned.
And for an example of where there was a serious bug. Patriot missile launchers were having a hard time tracking scuds (which they're trying to shoot down). This happened when they had been running for a "long" period of time (which the manufacturer warned about, but never said how long is "long"). What was happening, was that the radar, that was tracking the scud, would predict where the scud was going to be. And it would keep moving the radar dish to follow it. But for some reason the radar dish would overshoot the prediction and lose track of the scud. From what I could tell (and remember) from the description of the problem (I saw this on the history channel), basically the clock on the Patriot missile launcher ticked once every millisecond (I forget the exact time). And their code basically did.
void tick(){ //this is called once every millisecondtime+=0.001; //time represents how many seconds have passed since some point}

So after this had been called millions of times over some hours. The time was off enough that it messed up the calculations for tracking the scud. Now a better way would be to store the number of milliseconds that that have passed in a 64 bit int, then dividing that out when you needed the time in seconds (still not "perfect" (division by 1000) but close enough)

##### Share on other sites
Multiplying by powers of ten and casting as above changes the value actually in the floating point number. Those zeroes indicate that the rounding went well and you got the answer you should have. The problem is with the method you use to display the numbers, please refer to my first post:

If you are looking to display numbers with less precision this is definitely the way to go.

More clearly, use "cout.precision(2);" assuming you are writing in C++, or the method proposed by Cocalus using printf(...) if you are using C.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011320
• ### Who's Online (See full list)

There are no registered users currently online

×