Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 17th, 2006, 9:29 AM   #21
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The point is, if you make a number like 83.389999, you should leave it alone, as a number. Display it as 83.39 all you like. You cannot get every possible number as a float. You cannot force a number that can't be represented. If you absolutely insist that you need the capability you seek (not many uses would require such, not many professional coders would fail to realize that), then you need to move to a decimal library or similar.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 17th, 2006, 9:37 AM   #22
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
That depends on why, precisely, you care about representing 83.39 exactly. For example, if the value 83.39 is a value of currency, and is "83 dollars and 39 cents", then one solution would be to use two integers (one to hold the number of dollars, one to hold the number of cents). But if the value represents something else, you will probably need to do something different. Another option is to store the value as floating point, but control how it is output.
grumpy is offline   Reply With Quote
Old Jul 17th, 2006, 11:43 AM   #23
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
hmm well, let it be then... Ill think of something, thx for you all.

Great help guys.

take care
hipolito is offline   Reply With Quote
Old Jul 17th, 2006, 2:18 PM   #24
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
You should understand that float and double are floating point numbers.
It is impossible for them to store numbers like 83.39 exactly out to umpteen digits. That is NOT how computers work.

Read this article at wikipedia:
http://en.wikipedia.org/wiki/Floating_point

It explains why there is "slop" in these kinds of numbers.

The only alternative is to use BCD arithmetic. sourceforge has some BCD libraries, I don't know how good they are. You can also buy Oracle or get MySQL which both support BCD arithmetic operations to about 32 significant digits.
jim mcnamara is offline   Reply With Quote
Old Jul 17th, 2006, 3:47 PM   #25
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
Indeed. You're expecting a simple answer when none exists. Floating point variables, contrary to what people seem to expect, cannot not hold just any old real value. They hold a set of discrete values.

If I was to say you can only represent your data to three significant figures, and then give you data of 83.39 (which has 4 significant figures) you would drop one figure, and probably store it as either 83.3 or 83.4 (depending on whether you choose to round up or down). Once you've done that, there would then be no way you can get back to the actual value of 83.39. Floating point variables work the same way: the only difference is that they work in binary (base 2) rather than base 10 (decimal) so the set of values where the effect is obvious are different. One phenomenon is that 0.1 (1/10th decimal) and 0.01 (1/100th decimal) cannot be represented exactly in base 2, so some decimal values cannot be represented in floating point values. Changing to base 10 would not fix the problem -- it would only change the set of values where the effect becomes visible: try to represent the fraction 1/3 with only a finite number of decimal places, and you will encounter exactly the same problem.

Without knowing why you are getting your knickers in a twist over this, it is not possible to give a workable solution. If your data can be (in theory) any real data, then you will have to live with the imprecision of floating point or do your calculations by hand. I gave an option above, if the reason you want to represent 83.39 exactly is because you are working with currency (eg if the notion of a fraction of a cent is meaningless).
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC