I'm writing C# apps where I want the value of PI as a float, not a double. Currently I cast the value from double to float and end up with ugly (float) Math.PI casts all over the code.
Is there a built in PI function that returns a float?
EDIT: Why use a float instead of a double? I'm using a LOT of floats, and crunching a lot of numbers so smaller data == more fits in cache == better performance.
Not as far as I know. Just have your own constant:
const float PI = (float)Math.PI;
Starting with .NET Core 2.0 (.NET Standard 2.1) and upper Microsoft give access to using float type without hacks by including default Math constants and methods into MathF class of System namespace with all arugments and return types as floats.
Example usage:
using System;
class Class
{
public void Method()
{
float floatPi = MathF.PI;
float floatSin = MathF.Sin(floatPi * 2);
}
}
For further information see documentation on MSDN about MathF type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With