goby icon indicating copy to clipboard operation
goby copied to clipboard

Should we support Float#== ?

Open st0012 opened this issue 7 years ago • 4 comments

Float comparison is risky and is not a good practice, I think most programmers would agree this. So should we drop the support (or raise specialized error) when user calling this method, or we should just support it?

From my perspective supporting this method and telling user it's better not to use it against the core concept of Goby: No more best practices. So I don't want to support it actually, or at least change it to a special API so that user need to be aware what they're doing.

How do you think? @saveriomiroddi (actually @hachi8833 and @hanachin and me already have a discussion on Slack, so you can see what we already discussed in discussion channel).

st0012 avatar Oct 25 '17 07:10 st0012

@hachi8833 Suggests that we can convert floats into rational. But how to implement this another question I think.

st0012 avatar Oct 25 '17 07:10 st0012

Maybe using the rat data type in Go to support float or rational type data directly to Goby?

Alexius-Huang avatar Oct 25 '17 08:10 Alexius-Huang

This is an interesting idea, and somewhat audacious, but the consequences need to be extremely well thought out.

First, eliminating == implicitly eliminates also other arithmetic operators (>=, <=, !=).

Ultimately, floating point comparison is a legitimate operation (arguably dangerous, but still legitimate), so it's a matter of analyzing all the use cases.

For some cases, forcing precise arithmetic would require a considerable shift in mindset - this can be appropriate, but it requires careful design.

What would the users have to do when they need equality comparison?

Defaulting the number literals to precise (decimal) representation is interesting, but how would that affect arithmetic? What would 2.0 / 3 evaluate to? In scripting languages, the data type stays the same (eg. 1 / 3 = 0, and 1.0 / 3 = 0.333...), but there is no decimal representation of 2.0 / 3 - one would need to switch to rational, which evades this logic.

The idea it's interesting, but as I wrote, ultimately, you need to think 100000000000% the implications.

I think using rational numbers as default is not appropriate, since rational numbers are a related but different concept (see https://github.com/shopspring/decimal for examples).

64kramsystem avatar Oct 25 '17 09:10 64kramsystem

I think we should find some real world use cases of floats. Basically I only wrote web applications (or something relates to web applications) so I only have experience on decimal.

st0012 avatar Oct 26 '17 07:10 st0012