0

After researching, it became obvious that double variable may have wrong values and even if you print the variable as it is (without rounding or whatever), it may print a different value than the variable really has. Therefore you shouldn't use it as dictionary key. Just take this example of the screenshot. (All involved fields are type double. I solved it by casting the two variables in the red box each into integers and multiplying them by 100000: while ((int)(fCounter * 1000000) > (int)(TickSize * 1000000))) enter image description here

My question is, can you / should you use doubles for anything at all? I could imagine for temporarily holding a value for a print, it may be okay. But is there any other area where you could use them?

FtoB
  • 25
  • 6
  • They are fine depending on the level of precision you need in your calculations and if you keep in mind that certain decimal values cannot be exactly represented as a double and repeated math operations on them can lead to the values being slightly higher or lower than you'd expect by just doing the math on paper. – juharr Feb 26 '22 at 17:55
  • Right. And exactly this is my question. Depending on What are they fine? For what? I sorta lost confidence to use them anywhere anymore. It only causes calculation errors. So I wonder where I could still use them. – FtoB Feb 26 '22 at 18:04
  • if you want hight level of precision then you shold use Decimal instead of Double. – Lofem Feb 26 '22 at 18:06
  • 1
    All regular type will have errors. This is a basic limitation. You need to understand the reason and can then decide which to use. Rule of thumb: Use decimal is you care about decimals, e.g. when dealing with money. Otherwise double is better. Note that no type can hold e.g. 1/3 etc.. – TaW Feb 26 '22 at 18:06

0 Answers0