What is the purpose of Decimal.One, Decimal.Zero, Decimal.MinusOne in .Net

后端 未结 4 826
谎友^
谎友^ 2021-02-01 11:58

Simple question - why does the Decimal type define these constants? Why bother?

I\'m looking for a reason why this is defined by the language, not possible uses or effec

4条回答
  •  鱼传尺愫
    2021-02-01 12:18

    Those 3 values arghhh !!!

    I think they may have something to do with what I call trailing 1's

    say you have this formula :

    (x)1.116666 + (y) = (z)2.00000

    but x, z are rounded to 0.11 and 2.00, and you are asked to calculate (y).

    so you may think y = 2.00 - 1.11. Actually y equals to 0.88 but you will get 0.89. (there is a 0.01 in difference).

    Depends on the real value of x and y the results will varies from -0.01 to +0.01, and in some cases when dealing with a bunch of those trailing 1's, and to facilate things, you can check if the trailing value equals to Decimal.MinusOne / 100, Decimal.One / 100 or Decimal.Zero / 100 to fix them.

    this is how i've made use of them.

提交回复
热议问题