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

Possible issue with "println" or with control flow #2497

Closed
papamarkou opened this issue Mar 7, 2013 · 3 comments
Closed

Possible issue with "println" or with control flow #2497

papamarkou opened this issue Mar 7, 2013 · 3 comments

Comments

@papamarkou
Copy link
Contributor

I run the following naive "for" loop in order to check that I can iterate with a step size of my own preference (0.1 in this example):

for i = 1:0.1:2
println(i)
end

and I got the following output:

1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7000000000000002
1.8
1.9
2.0

I suspect it has to do with "println", because the comprehension

[i for i= 1:0.1:2]

prints out 1.7 instead of 1.7000000000000002:

11-element Float64 Array:
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0

This is not necessarily a bug, it may be my lack of understanding. I was simply puzzled, since I was expecting 1.7 to be printed via "println" instead of 1.7000000000000002.

@JeffBezanson
Copy link
Sponsor Member

Array elements are printed with less precision:

julia> a=[i for i= 1:0.1:2]
11-element Float64 Array:
 1.0
 1.1
 1.2
 1.3
 1.4
 1.5
 1.6
 1.7
 1.8
 1.9
 2.0

julia> a[8]
1.7000000000000002

We could possibly print vectors with full precision as a special case, since there is room for it.

@StefanKarpinski
Copy link
Sponsor Member

See also #2333. If we integrate some of those fixes to ranges, which I have lying around on a branch somewhere, then this problem actually won't occur since you will get precisely 1.7 to the best possible floating-point representation.

@papamarkou
Copy link
Contributor Author

Thank you both Stefan and Jeff, #2333 was helpful in understanding the source of the problem.

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

3 participants