6 ms·
What I didn’t know about Ruby Numbers
- thatguyagain 6y agoNice read. Just wanted to add to your part about Floats (decimals) and how they are not unique to Ruby. A lot of developers, specially self thought developers (myself included) are not aware of this and might never discover it unless you start working with currencies or something similar. I guess academically thought developers learn this in school. Anyway, it's an interesting topic imo :) https://floating-point-gui.de/basic/ https://floating-point-gui.de/basic/
- tomthecreator 6y agoI'm really sorry olivdums, but it seems I cannot read your blog post because it's behind Mediums paywall. :)
- tleb_ 6y agoRemoving cookies for medium domains solves this.
- merlinscholz 6y agoWhich isn't trivial to do on mobile browsers and shouldn't be necessary in the first place
- tleb_ 6y agoAnother solution is using a private/incognito tab
- chrisseaton 6y ago> But please don't post complaints about paywalls. Those are off topic.
- olivdums 6y agoSorry about that, as suggested in the comment you can delete your cookies to solve it :)
- tzs 6y agoI don’t understand the direction of the arrows in the class diagram. In some cases they are pointing from a class to one of its subclasses, but in others they point from a subclass to its parent class.
- top_sigrid 6y agoHuh, you're right. They are plain wrong, i guess. I don't think that an Object inherits from Numeric ;)
- olivdums 6y agoIt's ok I have fixed the graph now ! Thanks for your feedback !! :)
- top_sigrid 6y agoThanks for your response :) Just fyi for me still the old version is showing up, also with cache cleared.
- olivdums 6y agoThanks for your feedbacks !!! I just discovered that I made a mistake while drawing the chart ! I will fix this in a few hours thanks again !!
- olivdums 6y agoIt's ok I have fixed the graph ! Thanks again :)
- pqdbr 6y agoGreat article. Just one small tip: instead of using the BigDecimal("123.45") notation, I much prefer typing 123.45.to_d. Anywhere you used Floats, you can just replace them with BigDecimals. They work just the same. Some people get scared by them because they are always printed in scientific notation in the console, but you can just call `.to_s` on them to see them in a regular format: 123.45.to_d => 0.12345e3 123.45.to_s => "123.45" So don't get worried about the display. The behave exactly the same as a float, you can sum, multiply, and so on, and then you call .to_s when it's time to display it. Also any BigDecimal#round will give you an integer (just like .to_i will), but you can also call Bigdecimal#round(2) to round to, for instance, 2 decimal places. Also, if you are coding anything related to currencies, I strongly suggest you always use integers to store the amounts in cents. And when you need to divide it for calculations, just call `.to_d`, do your math, and then `.round` at the end, so you discard the fractions of cents. It works wonders, and the math will add up just like in Excel :)
- edflsafoiewq 6y agoBigDecimal("123.45") != 123.45.to_d. You should always prefer the former. In the latter, 123.45 is first read as a float, which introduces an error because it is not exactly representable as a float.
- pqdbr 6y agoRuby 2.7.2, just fire up irb: BigDecimal("123.45") == 123.45.to_d => true
- edflsafoiewq 6y agoShows you what I know. I thought I had tested this. I guess it keeps only a certain amount of precision.
- gioele 6y agoThat is just a coincidence (decimal ending in "5"), plus the fact that `.to_d` does some cleanup preprocessing. Counterexample: BigDecimal("1.5999999999999996") == 1.5999999999999996.to_d => false The most ergonomic way to initialize decimal numbers is IMO "123.45".to_d (Note the quotes around the number. `.to_d` is being called on a string.) BigDecimal's only lossless API is through strings. It would be nice to have native support for decimals at the parser level: 0d123.45 => BigDecimal("123.45")
- wwright 6y agoPedantic nitpick on floats: floats are exact. The issues with them are: - they are binary rather than decimal, so rounding behavior doesn’t match what humans expect - some floating point operations have rounding/error accumulation You can multiply and divide a float by 2 all day long and every result will be exact, with no error accumulation. However, adding floats together is a different story :-) That said, decimal types are still much preferable for anything that needs to be “exact” for human usage. It’s very easy to make a mistake with floats, even if they could support your use case.
- chrisseaton 6y ago> Pedantic nitpick on floats: floats are exact. I think you're meaning 'exact' as in they're deterministic. I don't know anyone who knows what they're talking about that argues against this, so I think you're not disagreeing with anyone. Other people mean 'not exact' as in they can't represent all real numbers. Which is true.
- lukeschlather 6y agoIntegers can't represent all real numbers either. Integers can represent more decimal numbers, which tends to be what humans use. But that doesn't make integers more exact. It does mean that integers will probably more accurately reflect the precision of your inputs. (But that presumes that your inputs are entered by humans, and not floats coming out of a sensor that produces readings as floats.)
- TheRealPomax 6y agocareful with your terminology there: integers are by definition not real numbers. All ints are reals, but only an infinitely small proportion of reals are ints. If, however, you meant "the integer datatypes can't represent all integers", then you probably want to say that instead of mixing maths terminology. And you'll also have to back up that claim because bigintegers are as accurate as you have space to store them in; their limitation is not imposed by their spec, but by your hardware. Unlike IEEE floating point decimal numbers, where the limitation is explicitly part of the spec itself. That limitation is literally why floats even work at all.
- nly 6y agoI was working on a problem yesterday at work where a remote system was sending prices (market data) to us in IEE754 binary doubles One of the test vectors was 5.80, but when we were printing them out to 15 digits of precision (the maximum a double is guaranteed to preserve) we were seeing "5.80000019073486", where of course we expected to see "5.8". As it turns out this is the closest to 5.8 you can get in single precision so the remote system was obviously sourcing these from a C 'float'. sigh
- chrisseaton 6y agoEveryone says that 'banks never use floating point to represent money'... but when you talk to people actually working in banks and other financial institutions and actually look at their code turns out they're using floating point all over the place.
- nly 6y agoI can tell you for a fact that at least one large data vendor uses floating point almost exclusively internally :-) The thing is, it's actually 'fine' until you actually need to do a calculation. Once you get there you need to convert to decimal and round to however many dp's you should be using.
- aldanor 6y agoHoly cow. Which market data provider in their sane mind would send decimals as floats? I don't think I've ever encountered binary market data using anything but fixed-point decimals (so essentially, integers) for representing prices.
- nly 6y agoTrading platforms using older tech (pretty much anyone not owned by the big groups) do this all the time.