开发者

Simple C# calculation - datatypes

开发者 https://www.devze.com 2022-12-09 09:12 出处:网络
Problably an easy questions but this开发者_StackOverflow calculation bothers me.. why is double test = 57.05 - 57 equal to 0.049999999999997158 and not 0.05? And which datatype should I use to get the

Problably an easy questions but this开发者_StackOverflow calculation bothers me.. why is double test = 57.05 - 57 equal to 0.049999999999997158 and not 0.05? And which datatype should I use to get the correct result?


Use a decimal. And read this.


Try to convert .05 decimal to binary and tell me what result you get (hint: it's a repeating value).


Long answer: Floating Point Representation

Short answer: types like double conform to IEEE 754, which dictates data types which may have a decimal point anywhere (floating). Because of the representation in the computer's native binary, the result of mathematical operations on these representations in many cases may only be an approximation of the actual result.


Floating point calculations are not as correct.

How about double-precision-problems-on-c-net

0

精彩评论

暂无评论...
验证码 换一张
取 消