c# - modulo function -


this question has answer here:

newbie in c#, trying work out simple calculation.

float old x=300    float distance=300    float pitch=0.8 int sign=1   

new x= old x - (sign * (distance % pitch) * 0.5 f)

the value generated program new x 299.6 (which don't understand).

the value (distance % pitch) 0.7999955. if calculate manually 300 modulo 0.8 0. guessing modulo function behaves differently float values don't know how. or calculated 300 percentage of 0.8?

explanation on appreciated.

never expect binary floating-point calculations (using float or double) on decimal floating point values (e.g. 0.8) give exact results.

the problem of decimal numbers cannot represented in binary floating-point format. can think of trying represent 1/3 in decimal notation. it's not possible.

in case, 0.8f not 0.8. if cast value double precision, see it's closer 0.800000011920929

float pitch = 0.8f; console.writeline((double)pitch);  // prints 0.800000011920929 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -