DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   noob: What and when you would use Decimal vs Float Type (http://www.daniweb.com/forums/thread95473.html)

jobs Nov 4th, 2007 4:02 pm
noob: What and when you would use Decimal vs Float Type
 
What and when you would use Decimal vs Float Type?

Duoas Nov 4th, 2007 4:53 pm
Re: noob: What and when you would use Decimal vs Float Type
 
Float types are just the hardware's floating point data type. Usually this is an IEEE single (4-bytes), double (8-bytes), or extended (10-bytes).

The problem with these is that they approximate numbers. Hence, they are not very exact. They lack precision and they are susceptible to the influences binary arithmetic.

The python decimal module gives a floating point class called Decimal which stores a number exactly. The tradeoff is that it is not as fast to use as a float. In most cases it is not so important to be so exact, so the float works just fine and there is no need to use Decimal.

However, in scientific computing exactness is often very important, so the Decimal type is very handy whenever it makes a difference between, say
1.000000000
and
1.000000001

Hope this helps.


All times are GMT -4. The time now is 7:00 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC