You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is less of an immediate issue and more of a thing that I'd like to see discussed.
As it turns out, since the recent floating point rework (which, on the whole, is great!) in d2bee10, math with --scale=0 will round instead of just truncating everything after the ".".
Apparently it rounds towards even (banker's rounding):
$ math-s0 10 / 4
2
$ math-s0 10 / 6
2
What I'd like to know is if anyone knows of a case where this will break? Is truncating better? Is there a need for an option?
I'm not sure about any of this, I'd just like to ideally have it nailed down for 3.0.
The scale argument corresponds directly to the printf precision format specifier '*', which rounds in an implementation-defined manner. I believe most implementations respect the current rounding mode, which defaults to round-towards-even. Personally I'm happy to offload this rounding decision to the implementation.
Personally I'm happy to offload this rounding decision to the implementation.
Isn't it nice to have "portable" math results, or is that a pipe dream anyway?
And even if we don't have results portable across different systems, wouldn't it be nice to have them compatible across fish version, at least with an option? I know that my battery-display-thingy would change behavior, and in a weird way, if I hadn't hacked it to use string instead of math (to do truncating division by 10).
Ok you're right. We don't want to be fielding bugs due to differences here. Probably it's sufficient to special case scale = 0....or maybe not, perhaps we should match bc and always truncate towards zero.
Probably it's sufficient to special case scale = 0
That would look like:
$ for s in (seq 6 -1 0); math-s$s 5 / 3; end
1.666667
1.66667
1.6667
1.667
1.67
1.7
1 # instead of 2
which at first glance seems okay? That would work for my battery use case.
It appears to me if you are using scale=0, then you want integer output, in which case truncating is expected (that's what python does with integer division).
On the other hand, this obviously means that the difference between scale=1 and scale=0 is at most almost 1. Which is large, but probably not important. I actually don't think many people will use any explicit scale other than 0. "--scale" is a bit of a holdover from bc, which didn't have any other way of forcing integer operations, so we implemented the full thing.
Okay, remind me again why we want to special-case 0?
What does that inconsistency gain us? At best, it seems like it's "unnecessary" to handle cases other than default-scale and integer-scale (then we should deprecate --scale, and include a --integer option instead), and at worst it makes it weirder to deal with small scales (e.g. --scale=1).
Presumably because the reason to use s0 is to seek behavior compatible with other tools and calculators, all of which default to truncation rather than rounding? Or, setting scale to zero implies integer division instead of floating point, and integer division should truncate as it does in (every?) programming language.
Presumably because the reason to use s0 is to seek behavior compatible with other tools and calculators, all of which default to truncation rather than rounding?
That's why we want to truncate, yes.
My question now is why that line of thinking applies just to s0. Why not just truncate always?
What about adding --trunc in addition to --scale? This would avoid the need for special casing. Integer division could be explicitly performed with --trunc=0.
Activity
ridiculousfish commentedon Oct 12, 2018
The scale argument corresponds directly to the printf precision format specifier '*', which rounds in an implementation-defined manner. I believe most implementations respect the current rounding mode, which defaults to round-towards-even. Personally I'm happy to offload this rounding decision to the implementation.
faho commentedon Oct 12, 2018
Isn't it nice to have "portable" math results, or is that a pipe dream anyway?
And even if we don't have results portable across different systems, wouldn't it be nice to have them compatible across fish version, at least with an option? I know that my battery-display-thingy would change behavior, and in a weird way, if I hadn't hacked it to use
stringinstead ofmath(to do truncating division by 10).ridiculousfish commentedon Oct 13, 2018
Ok you're right. We don't want to be fielding bugs due to differences here. Probably it's sufficient to special case scale = 0....or maybe not, perhaps we should match
bcand always truncate towards zero.faho commentedon Oct 13, 2018
That would look like:
which at first glance seems okay? That would work for my battery use case.
It appears to me if you are using scale=0, then you want integer output, in which case truncating is expected (that's what python does with integer division).
On the other hand, this obviously means that the difference between scale=1 and scale=0 is at most almost 1. Which is large, but probably not important. I actually don't think many people will use any explicit scale other than 0. "--scale" is a bit of a holdover from bc, which didn't have any other way of forcing integer operations, so we implemented the full thing.
mqudsi commentedon Oct 13, 2018
I think it's a fair compromise, and special casing 0 would have been my suggestion as well.
You're probably right on that account.
faho commentedon Oct 21, 2018
Okay, remind me again why we want to special-case 0?
What does that inconsistency gain us? At best, it seems like it's "unnecessary" to handle cases other than default-scale and integer-scale (then we should deprecate --scale, and include a --integer option instead), and at worst it makes it weirder to deal with small scales (e.g. --scale=1).
mqudsi commentedon Oct 21, 2018
Presumably because the reason to use s0 is to seek behavior compatible with other tools and calculators, all of which default to truncation rather than rounding? Or, setting scale to zero implies integer division instead of floating point, and integer division should truncate as it does in (every?) programming language.
faho commentedon Oct 21, 2018
That's why we want to truncate, yes.
My question now is why that line of thinking applies just to s0. Why not just truncate always?
lofidevops commentedon Oct 24, 2018
What about adding
--truncin addition to--scale? This would avoid the need for special casing. Integer division could be explicitly performed with--trunc=0.lofidevops commentedon Oct 24, 2018
(Or make
--scaledo truncation, and add--roundfor rounding.)faho commentedon Oct 24, 2018
Okay, I have now merged truncating for --scale=0.
If we want truncating always, we can still change it. But it's unlikely to matter.
Math: Truncate integers (scale == 0)