On Tue, 24 May 2016 04:16 am, Pete Forman wrote:

> Something else which I do not think has been stated yet in this thread
> is that floating point is an inexact representation. Just because
> integers and binary fractions have an exact correspondence we ought not
> to be affording them special significance. Floating point 1 is not the
> integer 1, it stands for a range of numbers some fraction either side of
> 1.

That's a matter of interpretation. Sometimes that is the case: if the result
of some calculation involving errors happens to result in 1.0 (rather than,
say, 1.00270128) by the merest coincidence, then there is nothing special
about 1.0 and it represents some interval, same as any other float in that
situation.

But if for some strange reason you decide to do integer maths using floats:

x = 14.0/2.0 - 1.0

then the result, x == 1.0, is exact (assuming that the quantities 14.0 etc.
represent exact constants rather than inexact measured quantities with
associated measurement error).

And:

py> math.sqrt(1)
1.0

is not a range of numbers, it is an exact result.

IEEE 754 floating point has the concept of exact and inexact calculations.
When it comes to floats, Python unfortunately has no reliable way to tell
whether a calculation was exact or inexact, but the decimal module does.


py> from decimal import *
py> with localcontext(Context(traps=[Inexact])):
...     Decimal(25).sqrt() - Decimal(2)**Decimal(2)
...
Decimal('1')

That decimal 1 is *exact*. There's no rounding error. Provided that the
constants themselves are free of any measurement error, there is no error
at all in this calculation. Also exact:

py> with localcontext(Context(traps=[Inexact])):
...     Decimal(10).log10()
...
Decimal('1')


Whereas this Decimal 1 is inexact:

py> d = Decimal("1.0000000000000000000000000001")
py> assert d != Decimal(1)
py> d.sqrt() == 1
True

We can trap the inexact calculation:

py> with localcontext(Context(traps=[Inexact])):
...     d.sqrt()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
decimal.Inexact: [<class 'decimal.Inexact'>]




-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to