goby-lang / goby

Goby - Yet another programming language written in Go
MIT License
3.49k stars 171 forks source link

Should we support Float#== ? #481

Open st0012 opened 6 years ago

st0012 commented 6 years ago

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 commented 6 years ago

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

Alexius-Huang commented 6 years ago

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

64kramsystem commented 6 years ago

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).

st0012 commented 6 years ago

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.