Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

float

A built-in type for floating-point numbers.

說明

The float built-in type is a 64-bit double-precision floating-point number, equivalent to double in C++. This type has 14 reliable decimal digits of precision. The maximum value of float is approximately 1.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 to float in C++, which have 6 reliable decimal digits of precision. For data structures such as Vector2 and Vector3, Godot uses 32-bit floating-point numbers by default, but it can be changed to use 64-bit doubles if Godot is compiled with the precision=double option.

Math done using the float 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 compare float values for equality.

Note: In a boolean context, a float will evaluate to false if it's exactly equal to 0.0, and to true otherwise.

教學

建構子

float

float()

float

float(from: float)

float

float(from: String)

float

float(from: bool)

float

float(from: int)

運算子

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-()


建構子說明

float float() 🔗

建構預設初始化的 float,會被設為 0.0


float float(from: float)

建構給定 float 的副本。


float float(from: String)

String 轉換為 float,遵循與 String.to_float() 相同的規則。


float float(from: bool)

bool 值轉換為浮點值,float(true) 將等於 1.0,float(false) 將等於 0.0。


float float(from: int)

int 值轉換為浮點值,float(1) 將等於 1.0


運算子說明

bool operator !=(right: float) 🔗

如果兩個浮點數彼此不同,則返回 true

注意:@GDScript.NAN 的行為與其他數位不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator !=(right: int) 🔗

如果整數的值與浮點數不同,則返回 true


Color operator *(right: Color) 🔗

Multiplies each component of the Color, including the alpha, by the given float.

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

Quaternion operator *(right: Quaternion) 🔗

將該 Quaternion 的每個分量乘以給定的 float。此操作本身沒有意義,但可以用作更大運算式的一部分。


Vector2 operator *(right: Vector2) 🔗

Multiplies each component of the Vector2 by the given float.

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

Vector2 operator *(right: Vector2i) 🔗

Multiplies each component of the Vector2i by the given float. Returns a Vector2.

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

Vector3 operator *(right: Vector3) 🔗

將該 Vector3 的每個分量乘以給定的 float


Vector3 operator *(right: Vector3i) 🔗

Multiplies each component of the Vector3i by the given float. Returns a Vector3.

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

Vector4 operator *(right: Vector4) 🔗

將該 Vector4 的每個分量乘以給定的 float


Vector4 operator *(right: Vector4i) 🔗

Multiplies each component of the Vector4i by the given float. Returns a Vector4.

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

float operator *(right: float) 🔗

將兩個 float 相乘。


float operator *(right: int) 🔗

floatint 相乘。結果是 float


float operator **(right: float) 🔗

float 提升到 float 次冪。

print(39.0625**0.25) # 2.5

float operator **(right: int) 🔗

float 提升到 int 次冪。結果為 float

print(0.9**3) # 0.729

float operator +(right: float) 🔗

將兩個浮點數相加。


float operator +(right: int) 🔗

float 加上 int。結果為 float


float operator -(right: float) 🔗

將一個浮點數減去另一個浮點數。


float operator -(right: int) 🔗

float 減去 int。結果為 float


float operator /(right: float) 🔗

將兩個浮點數相除。


float operator /(right: int) 🔗

float 除以 int。結果是 float


bool operator <(right: float) 🔗

如果左側的浮點數小於右側的,則返回 true

注意:@GDScript.NAN 的行為與其他數位不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator <(right: int) 🔗

如果該 float 小於給定的 int,則返回 true


bool operator <=(right: float) 🔗

如果左側的浮點數小於等於右側的,則返回 true

注意:@GDScript.NAN 的行為與其他數位不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator <=(right: int) 🔗

如果該 float 小於等於給定的 int,則返回 true


bool operator ==(right: float) 🔗

如果兩個浮點數完全相等,則返回 true

注意:由於浮點精度誤差,考慮改用更可靠的 @GlobalScope.is_equal_approx()@GlobalScope.is_zero_approx()

注意:@GDScript.NAN 的行為與其他數位不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator ==(right: int) 🔗

如果該 float 等於給定的 int,則返回 true


bool operator >(right: float) 🔗

如果左側的浮點數大於右側的,則返回 true

注意:@GDScript.NAN 的行為與其他數位不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator >(right: int) 🔗

如果該 float 大於給定的 int,則返回 true


bool operator >=(right: float) 🔗

如果左側的浮點數大於等於右側的,則返回 true

注意:@GDScript.NAN 的行為與其他數位不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator >=(right: int) 🔗

如果該 float 大於等於給定的 int,則返回 true


float operator unary+() 🔗

返回與 + 不存在時相同的值。單目 + 沒有作用,但有時可以使你的程式碼更具可讀性。


float operator unary-() 🔗

返回該 float 的相反數。如果為正數,則將該數變為負數。如果為負數,則將該數變為正數。對於浮點數,數位零既可以是正數,也可以是負數。