Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast int to float in F#

Tags:

f#

Learning F#, the syntax is still quite foreign to me. How do I cast this integer to float?

let add x y =     x + y  let j = 2 add 1.1 j 

In C# Float + int= Float

float j = 1.1f + 5; 
like image 239
NitroxDM Avatar asked Aug 19 '13 17:08

NitroxDM


People also ask

How do you cast an int to a float in Python?

To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.

Can you cast an int to a float in C?

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.

Can we convert int to float in C++?

You can't reassign i as a float after that. An int is always an int and will remain an int as long as it was declared as an int and will never be able to change into anything but an int.


2 Answers

EDIT: misread the question...

I'm pretty sure that the float() function would do the job:

add 1.1 (float 2) 
like image 197
feralin Avatar answered Nov 07 '22 05:11

feralin


First, the function you specified has type int->int->int which means it takes 2 ints and returns an int. If you want it to use floats you need to specify the type of one of the arguments:

let add (x : float) y = x + y //add : float->float->float 

As others have mentioned, you can cast to a float using the float() function:

float 2 //2 : float 

If you are using numeric literals like in your example, you can just use 2.0 instead of 2 which has the float type.

add 1.1 2.0 
like image 22
Wesley Wiser Avatar answered Nov 07 '22 05:11

Wesley Wiser