c# 3.0 - Why null-coalescing operator (??) in a mathematical function works different without parenthesis? -
hello have doubt mathematical function in c# , want know why happens. 2 lines give different results, why?
container.price * (1 + (container.tax_rate / 100m)) * (1 - (container.promotion_rate ?? 0 / 100m)) returns -> -839.9944
container.price * (1 + (container.tax_rate / 100m)) * (1 - ((container.promotion_rate ?? 0) / 100m)) returns -> 50.99966
and difference in function parenthesis on second function: (container.promotion_rate ?? 0)
the null-coalescing operator has lower precedence divide operator. therefore, code interpreted as
container.promotion_rate ?? (0 / 100m) which gives different result.
Comments
Post a Comment