jworld2
0
Light Poster
Recommended Answers
Jump to PostFloating point algorithms in all computer languages suffer from a small error as the floating point world meets the binary world. Representing -0.5 in just a sequence of 1 and 0 is not possible with true accuracy. Look at this ...
import math # use a list …
Jump to PostWit this:
import math print(math.cos((2*math.pi)/3)) # -0.5 print(0.5 + math.cos((2*math.pi)/3)) # 2.22044604925e-16 print(-0.5 + math.cos((2*math.pi)/3)) # -1.0 x = math.cos((2*math.pi)/3) print(x) # -0.5 print([x]) # [-0.4999999999999998] print([-0.5 + x]) # [-0.9999999999999998]
Strange?
It is not strange, because printing a float x prints str(x) in fact, while printing a list …
All 7 Replies
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
Gribouillis
1,391
Programming Explorer
Team Colleague
Gribouillis
1,391
Programming Explorer
Team Colleague
bumsfeld
413
Nearly a Posting Virtuoso
Gribouillis
1,391
Programming Explorer
Team Colleague
vegaseat
commented:
Thank you, that explains a lot
+13
Gribouillis
1,391
Programming Explorer
Team Colleague
jworld2
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.