On Sun, Sep 5, 2021 at 12:48 PM Hope Rouselle <hrouse...@jevedi.com> wrote: > > Chris Angelico <ros...@gmail.com> writes: > > > On Fri, Sep 3, 2021 at 4:58 AM Hope Rouselle <hrouse...@jevedi.com> wrote: > >> > >> Hope Rouselle <hrouse...@jevedi.com> writes: > >> > >> > Just sharing a case of floating-point numbers. Nothing needed to be > >> > solved or to be figured out. Just bringing up conversation. > >> > > >> > (*) An introduction to me > >> > > >> > I don't understand floating-point numbers from the inside out, but I do > >> > know how to work with base 2 and scientific notation. So the idea of > >> > expressing a number as > >> > > >> > mantissa * base^{power} > >> > > >> > is not foreign to me. (If that helps you to perhaps instruct me on > >> > what's going on here.) > >> > > >> > (*) A presentation of the behavior > >> > > >> >>>> import sys > >> >>>> sys.version > >> > '3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 > >> > bit (AMD64)]' > >> > > >> >>>> ls = [7.23, 8.41, 6.15, 2.31, 7.73, 7.77] > >> >>>> sum(ls) > >> > 39.599999999999994 > >> > > >> >>>> ls = [8.41, 6.15, 2.31, 7.73, 7.77, 7.23] > >> >>>> sum(ls) > >> > 39.60000000000001 > >> > > >> > All I did was to take the first number, 7.23, and move it to the last > >> > position in the list. (So we have a violation of the commutativity of > >> > addition.) > >> > >> Suppose these numbers are prices in dollar, never going beyond cents. > >> Would it be safe to multiply each one of them by 100 and therefore work > >> with cents only? For instance > > > > Yes and no. It absolutely *is* safe to always work with cents, but to > > do that, you have to be consistent: ALWAYS work with cents, never with > > floating point dollars. > > > > (Or whatever other unit you choose to use. Most currencies have a > > smallest-normally-used-unit, with other currency units (where present) > > being whole number multiples of that minimal unit. Only in forex do > > you need to concern yourself with fractional cents or fractional yen.) > > > > But multiplying a set of floats by 100 won't necessarily solve your > > problem; you may have already fallen victim to the flaw of assuming > > that the numbers are represented accurately. > > Hang on a second. I see it's always safe to work with cents, but I'm > only confident to say that when one gives me cents to start with. In > other words, if one gives me integers from the start. (Because then, of > course, I don't even have floats to worry about.) If I'm given 1.17, > say, I am not confident that I could turn this number into 117 by > multiplying it by 100. And that was the question. Can I always > multiply such IEEE 754 dollar amounts by 100? > > Considering your last paragraph above, I should say: if one gives me an > accurate floating-point representation, can I assume a multiplication of > it by 100 remains accurately representable in IEEE 754?
Humans usually won't give you IEEE 754 floats. What they'll usually give you is a text string. Let's say you ask someone to type in the prices of various items, the quantities thereof, and the shipping. You take strings like "1.17" (or "$1.17"), and you parse that into the integer 117. > Hm, I think I see what you're saying. You're saying multiplication and > division in IEEE 754 is perfectly safe --- so long as the numbers you > start with are accurately representable in IEEE 754 and assuming no > overflow or underflow would occur. (Addition and subtraction are not > safe.) > All operations are equally valid. Anything that causes rounding can cause loss of data, and that can happen with multiplication/division as well as addition/subtraction. But yes, with the caveats you give, everything is safe. ChrisA -- https://mail.python.org/mailman/listinfo/python-list