-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: math: add IsDivisible(float64, float64) bool
#26181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What about something like |
@ianlancetaylor possibly not, quite a simple solution too, thanks! I would still like to keep this proposal open to discuss if it's worth incorporating that logic(or any other according to some standard) into the go standard library as a nice-to-have feature if the proposal is accepted, though. |
I hope there is a better way, because it returns false negatives. The correct answer below is true because
(For bike shedding purposes, I also dislike the name |
The hacky solution I use can be found here For verbosity's sake, here's the solution: func IsDivisible(num float64, den float64) bool {
partsA := strings.Split(strconv.FormatFloat(num, 'f', -1, 64), ".")
partsB := strings.Split(strconv.FormatFloat(den, 'f', -1, 64), ".")
maxDecimalPlaces := 0.0
if len(partsA) == 2 {
maxDecimalPlaces = float64(len(partsA[1]))
}
if len(partsB) == 2 {
maxDecimalPlaces = math.Max(float64(len(partsB[1])), maxDecimalPlaces)
}
n := num * math.Pow(10, maxDecimalPlaces)
d := den * math.Pow(10, maxDecimalPlaces)
return int64(n)%int64(d) == 0
} I know it's not the most performant or efficient one, but it hasn't given me any false positives yet. |
@AyushG3112 that panics on inputs like |
@as ah I see. The source for my inputs is a JSON so I never tested that. |
@as actually, |
@AyushG3112 good catch, I don't think a function operating on float64 values should panic though. In this implementation, the operation Also Divides(0, n) returns true (try n = 144), which is questionable. |
@as technically, Maybe we could treat the denominator being 0 as a special case, and change the method signature to: func Divides(float64, float64) bool, error and return an error if needed. |
The IEEE754 floating point number 11.1 is not divisible by the IEEE754 floating point number 0.1 You are discussing about a function that is supposed to work with Go's float64 types, but also expect the same results you would get if you were manipulating purely ideal Real numbers. This is a bad idea. |
By that definition @ALTree It doesn't seem like this proposal is reasonable without some kind of fuzz parameter |
This is not well-defined, as the commenters have pointed out. IsDivisible couldn't possibly do better than math.Mod(x,y) == 0, which you can already write. (And doesn't give the right answer for 2000, 0.01, because 0.01 is more precisely 0.01000000000000000020816681711721685132943093776702880859375. |
In the application I'm working on, I came across a requirement to find out whether one float was divisible by another. I tried doing this by using the
math.Mod
andmath.Remainder
method to check if the result== 0.0
.As you can see in this playground sample, neither of those work perfectly. While I was able to hack away the problem by a rather cumbersome hack, I think it would be pretty useful if the go standard library provides a method to check if a float is divisible by another.
The text was updated successfully, but these errors were encountered: