![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#22 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#23 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
hmm well, let it be then... Ill think of something, thx for you all.
Great help guys. take care |
|
|
|
|
|
#24 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#25 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
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). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|