8 ms·
0.30000000000000004
- messel 16y agoThis is the type of thing that makes mathematicians flying tackle computer scientists. the CS: "Hey, it's round off error. Get used to it" the Mathematician: "Fix IT!!"
- xentronium 16y ago> This is the type of thing that makes mathematicians flying tackle computer scientists. Well, it's fixable. You lose your deal of performance in order to get some absolutely valid rational numbers representation. It's only that most tasks don't require this kind of precision.
- jules 16y agoAnd then you discover that you have a square root in your computation.
- xentronium 16y agoGood point, but rational numbers aren't closed under power/square root operation :)
- 3pt14159 16y agoEven then it doesn't matter fully. There are some languages with supported data types where you can actually write the equivalent of. x = 2 y = 2 1.upto 100 do x = x ** (1 / 2) end 1.upto 100 do x = x ** 2 end x == y # => true because it handles the math by only evaluating the operations at the very end.
- jules 16y agoWhich ones? It honestly seems unlikely to me that this would work well in practice.
- caf 16y agoIt's not "round off error", though. More like: "It's a base 2 fractional number with no exact decimal expansion with a finite number of digits. Display it in base 2 fractional form if you don't want to see an approximation."
- aliguori 16y agoWell, it's more like: Binary numbers are a countably infinite set. Decimal numbers are a countably infinite set. You can therefore map binary representation to decimal representation 1-1 with a simple mapping function. OTOH, floating point numbers are an uncountably infinite set. The only way to map binary numbers to floating point numbers is to map to a strict subset. The subset happens to be different when mapping binary to floating point and decimal to floating point when using IEEE754.
- mseebach 16y agoThe mathematician: That's fine for 22/7. Now, go fix your computer so it knows how to count to 0.3, kthxbye
- eru 16y agoAs a mathematician I do not value 0.3 above 22/7.
- caf 16y ago0.3 is a rational too, it's just 3/10. Anyway, mathematicians don't use numbers at all - debasing the equations by performing caculations with them is so.. gauche. Leave that to the physicists, chemists and engineers.
- eru 16y agoI saw the occasional number in my studies. We'd rather calculate with knows than numbers. (See http://en.wikipedia.org/wiki/Knot_theory http://en.wikipedia.org/wiki/Knot_theory)
- 16y ago
- DougBTX 16y agoWell, which data type would you like to store the value of PI in? I mean, you could re-calculate it to arbitrary precision every time you use it... just don't try to store it at "full" precision using floats or decimals.
- dagw 16y agoIf it where me I'd store the value of PI as a structure containing an IEEE double, a decimal representation of, say, 100 digits and a pointer to a function to that can evaluate PI to an arbitrary precision. Then I'd just just use PI as a symbol and do symbolic algebra for as long as possible. Don't evaluate it to anything until the user actually asks for a numeric representation. Once a numeric representation is required I'd pick a value from the above structure based on what has been asked for.
- voodootikigod 16y agoBrendan Eich (Creator of JS) discussed this here: http://www.aminutewithbrendan.com/pages/20101025 http://www.aminutewithbrendan.com/pages/20101025 as it pertains to JS, but it is similar for other languages that implement IEEE double precision numbers
- DougBTX 16y agoI think ECMA script uses double-precision 64-bit binary format IEEE 754 values for all number storage, or are there browser differences I don't know about which cause problems?
- jsvaughan 16y agoFrom Slashdot: http://developers.slashdot.org/story/10/05/02/1427214/What-Every-Programmer-Should-Know-About-Floating-Point-Arithmetic?from=rss&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+Slashdot/slashdot+(Slashdot) http://developers.slashdot.org/story/10/05/02/1427214/What-E... Every programmer forum gets a steady stream of novice questions about numbers not 'adding up.'...
- heresy 16y agoI have to question the value of CS educations when a post of this nature pops up every couple of weeks, as if it is news that floating point arithmetic as implemented today, is by its nature an approximation, and that this is more noticeable the less bits you have to play with, when working with values that can't be represented neatly. Financial arithmetic? Convert to smallest unit and use integers or the currency data type du jour in your language, and don't act surprised when operations on 32-bit floating point don't yield the intuitively correct values. If you understood the representation format, you'd understand why.
- rlpb 16y agoIt would be nice if more languages supported fractional rational number arithmetic as a built-in type. Converting into fractional units and back again shouldn't be as much of a pain as it is in most languages at the moment.
- 3pt14159 16y agoIt is easy in Ruby and Python.
- teamonkey 16y agoOne of the things I love about Scheme is not having to worry about number formats. It's a number, it is what it is. You never run out of digits, you never have to worry about precision errors. It removed a layer of stress I never knew existed.
- eru 16y agoActually you are talking about a specific implementation of Scheme. Probably PLT Scheme? An standard compliant implementation can just stick to floating point for everything as the bare miminum. PLT Scheme is the Scheme to go for, though.
- teamonkey 16y agoAny fully-compliant R5RS really. I'm talking about the concepts of exactness and the numerical tower in particular. The notion that an integer is a subset of a real number instead of a whole separate type is conceptually very nice. http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2.1 http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z...
- mseebach 16y agoIt gets funnier: http://ma.rtinseeba.ch/post/1344678900/double-considered-harmful http://ma.rtinseeba.ch/post/1344678900/double-considered-har...
- est 16y agoIs this IEEE-754 specific? If so, are there any IEEE-754 alternatives?
- gaius 16y agoIn any practical sense, no. The inconvenience is overwhelmingly offset by it working the same everywhere. And as others have said, you can sacrifice speed for it working "properly" anytime.
- daliusd 16y agoYes, that's http://en.wikipedia.org/wiki/IEEE_754-1985 http://en.wikipedia.org/wiki/IEEE_754-1985 specific. I like this explanation: http://docs.python.org/tutorial/floatingpoint.html#representation-error http://docs.python.org/tutorial/floatingpoint.html#represent... There is alternatives but don't forget that it is hardware issue not software. It looks like http://en.wikipedia.org/wiki/IEEE_754-2008 http://en.wikipedia.org/wiki/IEEE_754-2008 has decimal format thus it should address that.
- TorKlingberg 16y agoYes it is IEEE-754 specific. I think any binary floating point will have similar pitfalls though. Python has a Decimal type that can represent decimal values exactly. It must be imported first though. It is probably much slower than IEEE-754 floating point, but for many uses that is not an issue. http://docs.python.org/library/decimal.html http://docs.python.org/library/decimal.html
- masklinn 16y ago> If so, are there any IEEE-754 alternatives? Decimal arithmetics, which incurs a severe performance loss as IEE754 floats and doubles are implemented in hardware.
- acqq 16y agoThe alternative is a revision of IEEE 754 to include "decimal floating-Point arithmetic," which is apparently something "in progress" because the interests of those involved can't result in agreement. I'd really like to read anything from some insider. I know that IBM did the most of the work in that direction. Decimal Floating Point is the thing to have, but who know when that can happen in processors or in wider use. I know from Brendan Eich that there were attempts to add DFP in Javascript but that even there no agreement was achieved. Edit: or it was standardized but still not gaining acceptance: http://en.wikipedia.org/wiki/IEEE_754-2008 http://en.wikipedia.org/wiki/IEEE_754-2008
- hartror 16y agohttp://stackoverflow.com/search?q=0.30000000000000004 http://stackoverflow.com/search?q=0.30000000000000004
- eiji 16y agoFunny thing. Google has this page at rank 5 by now (after 42 mins).
- LaPingvino 16y agoHacker News has intrinsic value
- tzs 16y agoApple's Calculator app used to have an odd floating point rounding behavior (I filed a bug report and they fixed it): 14479.14 - 152.36 = (result is 14326.78) 1143 / 78 = (result is 14.6538461538461) 14479.14 - 152.36 = (result is 14326.7799999999884) Note that the first and third calculations are the same, yet they resulted in different displayed results! I never understood this bug. I understand floating point, so understand that some numbers are not exactly representable. However, it should at least still be consistent! The same calculation should give the same results every time.
- hxa7241 16y agoI suppose it depends how exact you are in defining 'same calculation' -- two possible causes of inconsistency are: * FP means the rules of algebra do not hold, so if the calculation is done in a different equivalent form, you can get a different result. * With Intel's old FPU unit, if a value is written to memory rather than staying in registers you can get a different result.
- SingAlong 16y agoI just tried this in javascript (via Firebug) out of curiosity and it's the same problem. 0.1 + 0.2 = 0.30000000000000004
- masklinn 16y ago> and it's the same problem. it's not a problem.
- ciupicri 16y agoIt's not a bug, it's a feature or better said it's a known issue which should be taken into account when writing a program that uses floating point numbers.
- pavel_lishin 16y agoI'd like to submit a bug: when I stare directly at the sun, my eyes begin to hurt. Also, there's this weird force pulling me towards the Earth, what's that all about?
- aidenn0 16y agoI consider it a bug in the specification that all numbers are floating-point in javascript.
- Cushman 16y agoPeople often say "Just use (x) decimal arithmetic system for important stuff like finances." Out of curiosity, I'm wondering how much trade you would have to be doing for floating-point imprecision to cause an actual problem. Taking 0.2+0.1 as an example and figuring an imprecision of $0.00000000000000004 per $0.30, figuring a loss of one cent as being significant, I have 0.01/(0.00000000000000004 / 0.3) = 7.5e13, or... seventy-five trillion dollars? Never mind that you're as likely to get 0.6+0.1 = 0.69999999999999996, which should roughly cancel out the error over time. This is basically just an aesthetic problem in finance, yes?
- flatline 16y agoThere is a term for this in mathematics and now I can't remember it, but with some systems, e.g. number crunching with large matrices, a very small change like this can magnify into huge differences and give you totally wrong results. It's definitely something you must be aware of and take into account.
- Cushman 16y agoYeah, I guess if you're doing more complex math with the figures than just addition and subtraction there's the possibility for magnification effects. I'd be interested to see an actual example from business, though.
- anigbrowl 16y agoDon't know about business, but when you're working with audio it's very common to multiply the contents of a stream against those of an array, such as a filter kernel with the coefficients computed in advance. With multiple channels which are being mixed in unpredictable ways, there's plenty of room for errors to propagate. Most of the time the only result is an imperceptible rise in noise, but it's not uncommon to have threshold-dependent routing of signal flow or for audio signals to used as input to modulation processors and vice versa. Every audio synthesis tool I've ever used had at least one bug resulting from error accumulation. They don't stand out very well in testing, but then people start reporting things like I was playing a tune with this patch and stopped to eat dinner, but when I came back my keyboard would only make a horrible noise, is it broken?
- gnuvince 16y agoI want a QPU unit!
- tung 16y agoPeople who see this fall into one of two kinds: those who are shocked by this, and those who are shocked by those who are shocked by this.
- maukdaddy 16y agoActually there's a third category of us who understand why it happens and understand why others don't understand.
- xtacy 16y agoIf you really want precision and you're dealing only with rational numbers, it's better to maintain a struct rational { u64 numerator, denominator; }; and do all calculations with it.
- protomyth 16y agoI remember the discussion on floating point and talking about BCD (we had assembler on and IBM/370), but I get the feeling BCDs are not talked about much anymore given some of the discussions I've had over the years. A related thing to watch out for is any arithmetic with units and how to deal with fractional conversions. This could cost you a lot of money or mess up and inventory if handled poorly.
- erikwiffin 16y agohttp://0.30000000000000004.com/ http://0.30000000000000004.com/ Because why not? I've populated it with some languages that I can convenient access to an interpreter for. If you post/send me .1 + .2 in any other languages, I'll try and put them up.
- waterhouse 16y agoRacket (aka PLT Scheme): $ racket Welcome to Racket v5.0.1. > (+ .1 .2) 0.30000000000000004 > (+ 1/10 2/10) 3/10 Steel Bank Common Lisp: $ sbcl This is SBCL 1.0.43 [...elided] * (+ .1 .2) 0.3 * (+ 1/10 2/10) 3/10
- erikwiffin 16y agoThanks, added.
- eru 16y agoGHC (Haskell): $ ghci GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help 0.1 Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> 0.1 + 0.2 0.30000000000000004 And just for fun with GHC's rational numbers: Prelude> :m + Data.Ratio Prelude Data.Ratio> (1 % 10) + (2 % 10) 3 % 10 Hugs (Haskell): $ hugs Hugs> 0.1 + 0.2 0.3 bc: $ bc 0.1 + 0.2 .3 Gforth: $ gforth 0.1e 0.2e f+ f. 0.3 ok dc: $ dc 0.1 0.2 + p .3
- erikwiffin 16y agoThanks, I've added them. Not only do I not have an interpreter for any of these languages handy, I've never even used them!
- eru 16y agoYou're welcome. bc and dc are probably installed on your Linux or Unix box. By the way, you should fix "Below are some examples of sending .1 + .2 to standard output in a variety of common languages." to "[...] in a variety of languages." It also seems like the names of bc and dc are non-capitalized. For the Haskell entry, please just shorten it to "0.1 + 0.2". The "Prelude>" thing is just a prompt for the REPL. ":m + Data.Ratio" loads the rational number module, please take the entry about Haskell's rational numbers out since yours is a page about floating point. (You might want to replace it with a comment, that Haskell supports rational numbers. But so do lots of languages in their libraries.) C would be a good addition. (Plus Fortran, Cobol, Ada and J.)
- DannoHung 16y agoIf only IEEE 754-2008 would gain traction...
- sdizdar 16y agohttp://docs.sun.com/source/806-3568/ncg_goldberg.html http://docs.sun.com/source/806-3568/ncg_goldberg.html However, my problem is that modern language are hiding these things for you.
- sfphotoarts 16y agoLeaving ego's aside, while the reasons for this are obvious to anyone who learned programming with binary and old school stuff like that, there is a whole genre (class) of programmer that has learned what is needed to build web sites and unless you have large scale issues (which you solve by hiring someone with old school CS skills) you can happily be competent and successful without every knowing about how languages implement floating point math. I think it is wrong to belittle these people because I have worked with them and sometimes, I have found, it is their skills that are more often more influential in the success of a product that the CS guy in the back room tweaking the slab allocator. Times have changed. It's no longer crucial for every one who deserves the title 'developer' to know about these kinds of language nuances.
- tmsh 16y agoWelcome to the best lectures of your life: http://webcast.berkeley.edu/course_details_new.php?seriesid=2010-B-26353&semesterid=2010-B http://webcast.berkeley.edu/course_details_new.php?seriesid=... also available via iTunes U. I'm currently listening to them on my commute. Note, if you do actually go through the whole course -- you'll need to listen to a different year for lecture 24 or so -- that one is skipped. One of the highlights of my day is actually coming home and re-looking up what he's talking about. (Oh, and of course you can just listen to the two Floating Point lectures. It has to do with the non-uniform -- or at least non-linearly uniform mapping of numbers, represented with a significand/mantissa and an exponent, onto the set of real numbers + the fact that the exponent used is in base 2, in the hardware, so the floating point numbers are spread about in a particular way. The difference between real numbers, as you tick up the odometer with each bit, varies depending on where you are in the number line (with big numbers, it's actually much more, with smaller numbers it's pretty minimal, but not unnoticeable as seen with this example. Does that make sense? Maybe I'm off about this... Anyway, still obviously recommend the lectures. And now, I'm going to read up more on ALUs and MUXs..)
- marcusfrex 16y agoirb(main):002:0> 0.1 + 0.2 => 0.30000000000000004