Raised This Month: $51 Target: $400
 12% 

Floats precision in C++


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
commonbullet
Veteran Member
Join Date: Oct 2005
Old 04-06-2007 , 01:51   Floats precision in C++
Reply With Quote #1

I guess this is the root of the 'problem' I'm getting to convert floats to strings.

In this crap example, I fail to understand why fl3 is 0.199951. Shouldn't it be 0.2 ?

PHP Code:
float fl1 1234.2f;
float fl2 1234.0f;
float fl3 fl2 fl1
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
SweatyBanana
BANNED
Join Date: Sep 2005
Location: LOL
Old 04-06-2007 , 02:04   Re: Floats precision in C++
Reply With Quote #2

Quote:
Originally Posted by commonbullet View Post
I guess this is the root of the 'problem' I'm getting to convert floats to strings.

In this crap example, I fail to understand why fl3 is 0.199951. Shouldn't it be 0.2 ?

PHP Code:
float fl1 1234.2f;
float fl2 1234.0f;
float fl3 fl2 fl1
It should be -0.2
SweatyBanana is offline
Send a message via AIM to SweatyBanana Send a message via Yahoo to SweatyBanana
commonbullet
Veteran Member
Join Date: Oct 2005
Old 04-06-2007 , 02:08   Re: Floats precision in C++
Reply With Quote #3

Ooops, anyways it's -0.199951.
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
schnitzelmaker
Senior Member
Join Date: Apr 2006
Location: HERE
Old 04-06-2007 , 03:01   Re: Floats precision in C++
Reply With Quote #4

Anywhere exist an thread where i have explained it a little better but i hope this helps also.

The Problem is with converting hex into floats. Some float numbers dont exist as hex.
As Example 1234.2 is in hex 0x449A4666, but 0x449A4666 is in float 1234.199951.

A trick to fix this is multiply the float before caculate it and divide it after caculate.
((1234.0*10.0)-(1234.2*10.0))/10.0
__________________
schnitzelmaker is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-06-2007 , 10:07   Re: Floats precision in C++
Reply With Quote #5

This is possibly a bit unrelated, but you might be interested in reading this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
__________________
hello, i am pm
PM is offline
commonbullet
Veteran Member
Join Date: Oct 2005
Old 04-06-2007 , 14:20   Re: Floats precision in C++
Reply With Quote #6

Thanks schnitzelmaker and PM (very interesting reading, looks like I'll take years trying to digest all that).

It seems that I came across an old issue which is not exclusively related to C/C++ as I had believed.
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:22.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode