Skip to content

math rounds instead of truncating #5251

Closed

Description

@faho

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.

Activity

  1. added this to the fish-3.0 milestone on Oct 11, 2018
  2. ridiculousfish commented on Oct 12, 2018

    @ridiculousfish
    Member

    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.

  3. faho commented on Oct 12, 2018

    @faho
    MemberAuthor

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

  4. ridiculousfish commented on Oct 13, 2018

    @ridiculousfish
    Member

    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.

  5. faho commented on Oct 13, 2018

    @faho
    MemberAuthor

    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.

  6. mqudsi commented on Oct 13, 2018

    @mqudsi
    Contributor

    I think it's a fair compromise, and special casing 0 would have been my suggestion as well.

    I actually don't think many people will use any explicit scale other than 0.

    You're probably right on that account.

  7. faho commented on Oct 21, 2018

    @faho
    MemberAuthor

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

  8. mqudsi commented on Oct 21, 2018

    @mqudsi
    Contributor

    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.

  9. faho commented on Oct 21, 2018

    @faho
    MemberAuthor

    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?

  10. lofidevops commented on Oct 24, 2018

    @lofidevops

    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.

  11. lofidevops commented on Oct 24, 2018

    @lofidevops

    (Or make --scale do truncation, and add --round for rounding.)

  12. faho commented on Oct 24, 2018

    @faho
    MemberAuthor

    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.

  13. added a commit that references this issue on Nov 24, 2018
    119f9e2
  14. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions