Click here to Skip to main content
15,886,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have the following issue:

C#
float increment = 0.00025f;
float sampleFreq = 1.f / increment;
[+] sampleFreq = 3999.99976
and not 4000 why ?

How may I have the 4000 expected result (not via a rounding method) ?

Certainly something stupid.

Thank you very much in advance.
MiQi

What I have tried:

I have tried with double, same result.
Posted
Updated 14-Feb-19 7:32am
v3

It's a bit mathematical, but floats and doubles are floating point types which have a base 2 exponent as opposed to the base 10 we humans are used to. Some numbers can't be represented exactly in this (and also in our base 10 for that matter, think 1/3 = 0.33333333333....) and where that happens you get some accuracy loss.

This is typical of working with floating point numbers, you have to accept a margin of error and be careful with things like equality operators.

You could try using the decimal type in .NET. This uses a base 10 exponent so behaves the way you would expect it too (mostly), but it comes with a cost in terms of footprint and performance.
 
Share this answer
 
Comments
SuperMiQi 14-Feb-19 8:46am    
Thank you very much that is the solution. I get now the right expected result.
Because you're using the float data type. It can't represent floating point numbers exactly. Read up on it: here[^] and here[^] .

Think about it. How you do represent an infinite number of values in a finite number of bits? You can't. Sacrifices have to be made to make a data type that can represent a wide range of values that are most commonly used and offer some degree of "accuracy".

You can use the decimal type[^] instead. It's a 128-bit type, made for higher accuracy for things like financial purposes, but has a smaller range of values it can represent.
 
Share this answer
 
The precision of float and double is not sufficient.
decimal increment = 0.00025M;
decimal sampleFreq = 1M / increment;

Use decimal instead.
 
Share this answer
 
Comments
SuperMiQi 14-Feb-19 8:46am    
Thank you very much that is the solution. I get now the right expected result.
TheRealSteveJudge 14-Feb-19 8:50am    
You're welcome. Another one downvoted my solution. :-(
TheRealSteveJudge 14-Feb-19 8:47am    
WTF? My solution is right. Thank you for downvoting!
SuperMiQi 14-Feb-19 8:50am    
Hm. it was far from my intention, I accepted both comment to thanks everybody.
SuperMiQi 14-Feb-19 8:52am    
ah la la, how to fix that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900