Comments (5)
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.
so there is no solution to achive what I want?
from gox.
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.
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.
Thanks for your answers
from gox.
Related Issues (7)
- Licensing and Attribution HOT 1
- An Apache 2.0 licence but no copyright information HOT 3
- Test case of randx.RandomIntWeight not correct in go playground https://go.dev/play/p/XiLXecuRZDc HOT 2
- Tests fail to run on 32-bit architectures HOT 1
- format does not work on 28th of February HOT 5
- error with opening link in windows using `func OpenDefault(fileOrURL string) error`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gox.