Giter Site home page Giter Site logo

Comments (5)

icza avatar icza commented on May 27, 2024 1

The problem is with IEEE-754 standard. The numbers you pass to mathx.Round() are not the ones you think they are because they cannot be represented exactly with the IEEE-754 standard, only a very close, smaller number.

Let's increase the precision to 20 fraction digits to see more clearly:

func print(f, unit, exp float64) {
    rounded := mathx.Round(f, unit)
    fmt.Printf("input: %.20f | got: %.3f | expected: %.3f\n", f, rounded, exp)
}

This will output (try it on the Go Playground):

input: 2.34500000000000019540 | got: 2.350 | expected: 2.350
input: 1.27499999999999991118 | got: 1.270 | expected: 1.280
input: 4.93499999999999960920 | got: 4.930 | expected: 4.940

As you can see, the number you pass is 1.2749999..., and this rounded to 2 decimal places is 1.27. And the other, the number you pass is 4.9349999..., and this rounded to 2 decimal places is 4.93.

matx.Round() doesn't need fixing, you're just not passing the numbers you think you are.

from gox.

Dmitrevicz avatar Dmitrevicz commented on May 27, 2024

so there is no solution to achive what I want?

from gox.

icza avatar icza commented on May 27, 2024

You want to pass 1.275 to mathx.Round() and expect to get 1.28. But you can't pass 1.275 as a float64 because there is no float64 value being exactly 1.275.

from gox.

icza avatar icza commented on May 27, 2024

In decimal base you can write 1.275 with finite digits, because this is a "nice" number in base 10. But there is no finite representation of 1.275 in the binary IEEE-754 standard.

Just like you can't write 1 / 3 with finite digits in decimal base, as it equals to 0.3333333......

from gox.

Dmitrevicz avatar Dmitrevicz commented on May 27, 2024

Thanks for your answers

from gox.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.