Skip to content
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

Sequences aren’t computed properly #24357

Closed
fbruetting opened this issue Oct 27, 2017 · 6 comments
Closed

Sequences aren’t computed properly #24357

fbruetting opened this issue Oct 27, 2017 · 6 comments

Comments

@fbruetting
Copy link
Contributor

fbruetting commented Oct 27, 2017

I want to generate substeps of 0:2π, but if I set the stepping to 2π/100, I get wrong results.

This works well with 98/99/101/102 steps, but not with 25/50/200… O_o

> (0:2π/100:2π) / 2π
0.0:0.010000000000000002:0.9900000000000002

I run julia 0.6.1 with everything up to date.

@pablosanjose
Copy link
Contributor

I believe this is expected. Use linspace(0,2π,100)/2π instead.

@andreasnoack
Copy link
Member

andreasnoack commented Oct 27, 2017

If you search the Julia history, you'd find that floating point ranges have been heavily discussed and (re-)adjusted over the years. If you search the Python history you can also find some lengthy discussions of the topic. Our current implementation assumes that the inputs are relatively-small-denominator rationals. This assumption produces

julia> length(0.1:0.1:0.3)
3

which most people prefer despite the fact that

julia> 3*0.1 <= 0.3
false

Personally, I think that this is too magic and too different from how floating point math normally works and I'd prefer a solution similar the one suggested by @pablosanjose (although I think it should be 101 not 100).

@StefanKarpinski
Copy link
Sponsor Member

Our current implementation assumes that the inputs are relatively-small-denominator rationals.

This is not an accurate description of what it does, but I'm rather tired of explaining this.

@fbruetting
Copy link
Contributor Author

fbruetting commented Oct 27, 2017

I cannnot understand this at all. Do you have a link for me / would you include some description in ?StepRangeLen?

And would it maybe be useful, outputting a warning, if the used step differs from the specified one? Otherwise this can lead to errors, the users aren’t aware of.

@pablosanjose
Copy link
Contributor

pablosanjose commented Oct 27, 2017

Some nice reading material related to this: #5636, #18777 and references therein
[EDIT: also #5585 !]

@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Oct 27, 2017

Long story short, is not mathematical 2π and couldn't possibly be since 2π is a transcendental number. Instead is the Float64 value closest to 2π. Similarly, 2π/100 is not the mathematical value 2π/100 – it's the closest Float64. The former is not exactly 100 times the latter:

julia> 2π < 100*(2π/100)
true

julia> 2π / (2π/100)
99.99999999999999

Julia is actually taking your requested values completely literally – they just aren't what you imagine them to be. @andreasnoack is scapegoating rational lifting (again), whereas that lifting is NOT happening here, so it cannot be at fault:

julia> collect(0:2π/100:2π) == [(2π/100)*k for k = 0:99]
true

Y'all are just mad at floating-point arithmetic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants