Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "System.Math.Cos" return a (float)?

In C# it bugs me how there is no "Math.Cos" function that returns a float. A double is the only value you can get back thus forcing you to cast it to a float. Like so:
float val = (float)Math.Cos(someVal);

I need to use floats because i'm doing stuff in Direct3D which strictly uses floats. Floats are much more common in the graphics world(as it stands now) because they are 32bit.

Is there any functionality within C# I can use that would simply just process floats like C++ can do??

I do not want to wrap any C++ stuff because this needs to run on XNA & Linux for OpenGL.

NOTE: It would nice to have code that did not cast a double to a float.

like image 722
zezba9000 Avatar asked Dec 03 '10 21:12

zezba9000


People also ask

Is math pi double or float?

In this case pi (3.141592653589793), has been encoded into the double precision floating point number. Note that the true value of this double precision number is 3.14159265358979311599796346854. There are multiple ways to store a decimal number in binary with a varying level of precision.

Is math Pi a float?

The well-known 3.14159265358979... value (Read Only). The ratio of the circumference of a circle to its diameter. Note that this value is a 32-bit floating point number i.e. a float .

How do you use Cos in C#?

Cos() is an inbuilt Math class method which returns the cosine of a given double value argument(specified angle). Parameter: num: It is the angle(measured in radian) whose cosine is to be returned and the type of this parameter is System.


1 Answers

Starting with .NET Core 2.0 (.NET Standard 2.1) (C# 8.0) and upper this is possible thing as built-in function.

You can just use MathF class with built-in constants or functions working with float type.

Example:

float cos = MathF.Cos(MathF.PI);

For further information see documentation on MSDN about MathF type.

like image 178
picolino Avatar answered Oct 04 '22 03:10

picolino