float

A built-in type for floating-point numbers.

Description

Thefloat built-in type is a 64-bit double-precision floating-point number, equivalent todouble in C++. This type has 14 reliable decimal digits of precision. The maximum value offloat is approximately1.79769e308, and the minimum is approximately-1.79769e308.

Many methods and properties in the engine use 32-bit single-precision floating-point numbers instead, equivalent tofloat in C++, which have 6 reliable decimal digits of precision. For data structures such asVector2 andVector3, Godot uses 32-bit floating-point numbers by default, but it can be changed to use 64-bit doubles if Godot is compiled with theprecision=double option.

Math done using thefloat type is not guaranteed to be exact and will often result in small errors. You should usually use the@GlobalScope.is_equal_approx() and@GlobalScope.is_zero_approx() methods instead of== to comparefloat values for equality.

Tutorials

Constructors

float

float()

float

float(from:float)

float

float(from:String)

float

float(from:bool)

float

float(from:int)

Operators

bool

operator !=(right:float)

bool

operator !=(right:int)

Color

operator *(right:Color)

Quaternion

operator *(right:Quaternion)

Vector2

operator *(right:Vector2)

Vector2

operator *(right:Vector2i)

Vector3

operator *(right:Vector3)

Vector3

operator *(right:Vector3i)

Vector4

operator *(right:Vector4)

Vector4

operator *(right:Vector4i)

float

operator *(right:float)

float

operator *(right:int)

float

operator **(right:float)

float

operator **(right:int)

float

operator +(right:float)

float

operator +(right:int)

float

operator -(right:float)

float

operator -(right:int)

float

operator /(right:float)

float

operator /(right:int)

bool

operator <(right:float)

bool

operator <(right:int)

bool

operator <=(right:float)

bool

operator <=(right:int)

bool

operator ==(right:float)

bool

operator ==(right:int)

bool

operator >(right:float)

bool

operator >(right:int)

bool

operator >=(right:float)

bool

operator >=(right:int)

float

operator unary+()

float

operator unary-()


Constructor Descriptions

floatfloat()🔗

Constructs a default-initializedfloat set to0.0.


floatfloat(from:float)

Constructs afloat as a copy of the givenfloat.


floatfloat(from:String)

Converts aString to afloat, following the same rules asString.to_float().


floatfloat(from:bool)

Cast abool value to a floating-point value,float(true) will be equal to 1.0 andfloat(false) will be equal to 0.0.


floatfloat(from:int)

Cast anint value to a floating-point value,float(1) will be equal to1.0.


Operator Descriptions

booloperator !=(right:float)🔗

Returnstrue if two floats are different from each other.

Note:@GDScript.NAN doesn't behave the same as other numbers. Therefore, the results from this operator may not be accurate if NaNs are included.


booloperator !=(right:int)🔗

Returnstrue if the integer has different value than the float.


Coloroperator *(right:Color)🔗

Multiplies each component of theColor, including the alpha, by the givenfloat.

print(1.5*Color(0.5,0.5,0.5))# Prints (0.75, 0.75, 0.75, 1.5)

Quaternionoperator *(right:Quaternion)🔗

Multiplies each component of theQuaternion by the givenfloat. This operation is not meaningful on its own, but it can be used as a part of a larger expression.


Vector2operator *(right:Vector2)🔗

Multiplies each component of theVector2 by the givenfloat.

print(2.5*Vector2(1,3))# Prints (2.5, 7.5)

Vector2operator *(right:Vector2i)🔗

Multiplies each component of theVector2i by the givenfloat. Returns aVector2.

print(0.9*Vector2i(10,15))# Prints (9.0, 13.5)

Vector3operator *(right:Vector3)🔗

Multiplies each component of theVector3 by the givenfloat.


Vector3operator *(right:Vector3i)🔗

Multiplies each component of theVector3i by the givenfloat. Returns aVector3.

print(0.9*Vector3i(10,15,20))# Prints (9.0, 13.5, 18.0)

Vector4operator *(right:Vector4)🔗

Multiplies each component of theVector4 by the givenfloat.


Vector4operator *(right:Vector4i)🔗

Multiplies each component of theVector4i by the givenfloat. Returns aVector4.

print(0.9*Vector4i(10,15,20,-10))# Prints (9.0, 13.5, 18.0, -9.0)

floatoperator *(right:float)🔗

Multiplies twofloats.


floatoperator *(right:int)🔗

Multiplies afloat and anint. The result is afloat.


floatoperator **(right:float)🔗

Raises afloat to a power of afloat.

print(39.0625**0.25)# 2.5

floatoperator **(right:int)🔗

Raises afloat to a power of anint. The result is afloat.

print(0.9**3)# 0.729

floatoperator +(right:float)🔗

Adds two floats.


floatoperator +(right:int)🔗

Adds afloat and anint. The result is afloat.


floatoperator -(right:float)🔗

Subtracts a float from a float.


floatoperator -(right:int)🔗

Subtracts anint from afloat. The result is afloat.


floatoperator /(right:float)🔗

Divides two floats.


floatoperator /(right:int)🔗

Divides afloat by anint. The result is afloat.


booloperator <(right:float)🔗

Returnstrue if the left float is less than the right one.

Note:@GDScript.NAN doesn't behave the same as other numbers. Therefore, the results from this operator may not be accurate if NaNs are included.


booloperator <(right:int)🔗

Returnstrue if thisfloat is less than the givenint.


booloperator <=(right:float)🔗

Returnstrue if the left float is less than or equal to the right one.

Note:@GDScript.NAN doesn't behave the same as other numbers. Therefore, the results from this operator may not be accurate if NaNs are included.


booloperator <=(right:int)🔗

Returnstrue if thisfloat is less than or equal to the givenint.


booloperator ==(right:float)🔗

Returnstrue if both floats are exactly equal.

Note: Due to floating-point precision errors, consider using@GlobalScope.is_equal_approx() or@GlobalScope.is_zero_approx() instead, which are more reliable.

Note:@GDScript.NAN doesn't behave the same as other numbers. Therefore, the results from this operator may not be accurate if NaNs are included.


booloperator ==(right:int)🔗

Returnstrue if thefloat and the givenint are equal.


booloperator >(right:float)🔗

Returnstrue if the left float is greater than the right one.

Note:@GDScript.NAN doesn't behave the same as other numbers. Therefore, the results from this operator may not be accurate if NaNs are included.


booloperator >(right:int)🔗

Returnstrue if thisfloat is greater than the givenint.


booloperator >=(right:float)🔗

Returnstrue if the left float is greater than or equal to the right one.

Note:@GDScript.NAN doesn't behave the same as other numbers. Therefore, the results from this operator may not be accurate if NaNs are included.


booloperator >=(right:int)🔗

Returnstrue if thisfloat is greater than or equal to the givenint.


floatoperator unary+()🔗

Returns the same value as if the+ was not there. Unary+ does nothing, but sometimes it can make your code more readable.


floatoperator unary-()🔗

Returns the negative value of thefloat. If positive, turns the number negative. If negative, turns the number positive. With floats, the number zero can be either positive or negative.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.