4 ms·
There are situations where it's very useful. I've used similar modules for code doing financial calculations. Because Ruby has weakly typed numerics, it's quit
by kscaldef 17y ago
There are situations where it's very useful. I've used similar modules for code doing financial calculations. Because Ruby has weakly typed numerics, it's quite easy to inadvertently coerce a value to Float. Once this happens you will start seeing, as you say, obscure bugs from floating point errors. The location where the bug manifests can be entirely different from the location in the code where the type coercion occurred. Placing type constraints on functions involved in these calculations seems quite warranted.
I wouldn't use this module on every function you write, and it doesn't seem like the author is advocating that. But strategically enforcing the expected types in critical portions of your codebase can be valuable.
- blasdel 17y agoIt would be 'weak' typing if Ruby ran an integer routine on a raw mantissa|significand value. Ruby is effectively doing type-checks on every operation, and that can never be 'weak' What you're complaining about is that it has a numeric tower, but does not have anything like type-classes.
- kscaldef 17y agoNo, I'm complaining that it has implicit type coercions, which is one flavor of weak typing. (I suppose, I would also complain that it does implicit coercions in a way which results in loss of precision.)
- arohner 17y agoThe problem is that ruby implicitly casts in a way that loses precision, exactly the opposite of what the author (may) desire. Call it whatever you want. 2**100 => 1267650600228229401496703205376 > (2**100).class => Bignum > (2**100)*0.5 => 6.33825300114115e+29 > ((2**100)*0.5).class => Float