简体   繁体   中英

Math.Pow vs scientific notation

I would like to know if there is difference between writing Math.Pow(10, -3) and 1E-3 . When I Console.WriteLine both values I get the same output, so is there a reason to prefer one over another?

When you call Math.Pow(10, -3) then .NET will calculate the value each time and calculation will affect your performance because this method works for double-precision floating-point number as power.

1E-3 is just a number so there is no need to calculate it in runtime.

It's better and more clearly to use numeric literals instead of multiply operator and it's better to use multiply operator instead of Math.Pow when it's possible.

Here is a benchmark that shows that numeric literal requires almost zero time to execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM