Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

PSoC™ 5, 3 & 1 Forum Discussions

Anonymous
Not applicable
0 Likes
1 Solution

This is caused by the internal representation of floats. The mantissa is expressed as  a fraction of 2^n which sometimes cannot be represented without an error (as 1/3 in decimal). The printf() functions round the argument to the required precision so you might not notice any difference. The only consequence for programmers is: Do not test a float for zero (0.0) but abs(float) for a selected epsilon.

Hint: Calculate Sum(1/n) for n = 1 to 100,000 and for n = 100,000 down to 1 and compare the results.

Bob

View solution in original post

2 Replies
MichaelF_56
Moderator
Moderator
Moderator
250 sign-ins 25 comments on blog 10 comments on blog

texnos

Moving to the PSoC 5 community.

0 Likes

This is caused by the internal representation of floats. The mantissa is expressed as  a fraction of 2^n which sometimes cannot be represented without an error (as 1/3 in decimal). The printf() functions round the argument to the required precision so you might not notice any difference. The only consequence for programmers is: Do not test a float for zero (0.0) but abs(float) for a selected epsilon.

Hint: Calculate Sum(1/n) for n = 1 to 100,000 and for n = 100,000 down to 1 and compare the results.

Bob