3 ms·
.125 + .375 == .5 You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.
by jmalicki 8mo ago
.125 + .375 == .5
You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.
- Sharlin 8mo ago> 0.1 just isn't an actual number. A finitist computer scientists only accepts those numbers as real that can be expressed exactly in finite base-two floating point?
- tux3 8mo ago0.1 is of course a real number, but let A \in R the set of actual numbers... (/s)
- JohnKemeny 8mo agoShould be \subset.
- jmalicki 8mo ago\infty is an actual number not in R
- Sharlin 8mo agoThe funny thing is, according to infinitists real numbers are not real. But I do like the concept of the set of actual numbers.
- hnfong 8mo agoI don't know whether I'm an infinitist, but I personally think "real numbers" is the most ingenious marketing term created by mathematicians...
- 1718627440 8mo agoThat's essentially what you already do for integer arithmetic.
- SideQuark 8mo agoYes. A computer scientist should know how numbers are represented and not expect non-representable numbers in that format to be representable. 0.1 is just as non-representable in floating point as is pi as is 100^100 in a 32 bit integer. Terminating dyadic rationals (up to limits based on float size) are the representable values.
- Sharlin 7mo agoI’m not sure if you got my joke but I referred to the mathematical philosophy, finitism.
- emil-lp 8mo agoAre you saying that my students should memorize which numbers are actual floats and which are not? > 1.25 * 0.1 0.1250000000000000069388939039
- jmalicki 8mo ago> Are you saying that my students should memorize which numbers are actual floats and which are not? Yes.
- kccqzy 8mo agoThey shouldn’t “memorize” this per se, but it should take them only a few seconds to work out in their head.
- stephencanon 8mo agoYour students should be able to figure out if a computation is exact or not, because they should understand binary representation of numbers.
- dehrmann 8mo agoTell them that they can only store integer powers of 2 and their sums exactly. 2^0 == 1. 2^-2 == .25. Then say it's the same with base 10. 10^-1 == 0.1. 1/9 isn't a power of 10, you you can't have an exact representation.
- SideQuark 8mo agoIf they were taught what was representable and why they’d learn it quickly. And those that forget details later know to chase it down again if they need it. Making it voodoo hides that it’s learnable, deterministic, and useful to understand.
- jmalicki 8mo ago(Another small note.... 1.25 * 0.1 is not representable because 0.1 is not representable, so that doesn't divide by 10) 1.25 = 2^0 + 2^-2, so is representable. 0.125 = 2^-3, so is representable 1.25 / 10.0 = 0.125 so is representable. 10.0 = 2^3 + 2^1. 1.25 * 0.1 is not representable, because 0.1 is not representable, and those low order bits show up in the multiplication