Corresponds to C float.
core :: Float :: angle_lerp
Linear interpolation of between the anglesa
and b
, in radians
core :: Float :: angle_normalize
Normalize theself
angle in radians to be within [-pi .. pi[
core :: Float :: to_hexa_exponential_notation
Returns the hexadecimal (String
) representation of self
in exponential notation
core :: Float :: to_precision
String
representation of self
with the given number of decimals
json :: serialization_write $ Float :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONmsgpack :: serialization_write $ Float :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackandroid :: bundle $ Float :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
noise :: noise $ Float :: core_serialize_to
Actual serialization ofself
to serializer
noise :: noise $ Float :: from_deserializer
Create an instance of this class from thedeserializer
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Float :: angle_lerp
Linear interpolation of between the anglesa
and b
, in radians
core :: Float :: angle_normalize
Normalize theself
angle in radians to be within [-pi .. pi[
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Comparable :: defaultinit
sqlite3 :: Sqlite3Data :: defaultinit
core :: Object :: defaultinit
core :: Numeric :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
core :: Float :: to_hexa_exponential_notation
Returns the hexadecimal (String
) representation of self
in exponential notation
core :: Float :: to_precision
String
representation of self
with the given number of decimals
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
serialization :: DirectSerializable
Instances of this class are not delayed and instead serialized immediately
# Native floating point numbers.
# Corresponds to C float.
universal Float
super Numeric
redef type OTHER: Float
redef fun object_id is intern
redef fun ==(i) is intern
redef fun !=(i) is intern
redef fun output is intern
redef fun <=(i) is intern
redef fun <(i) is intern
redef fun >=(i) is intern
redef fun >(i) is intern
redef fun +(i) is intern
redef fun - is intern
redef fun -(i) is intern
redef fun *(i) is intern
redef fun /(i) is intern
redef fun to_i is intern
redef fun to_f do return self
redef fun to_b is intern
redef fun zero do return 0.0
redef fun value_of(val) do return val.to_f
redef fun <=>(other)
do
if self < other then
return -1
else if other < self then
return 1
else
return 0
end
end
redef fun is_between(c, d)
do
if self < c or d < self then
return false
else
return true
end
end
# Compare float numbers with a given precision.
#
# Because of the loss of precision in floating numbers,
# the `==` method is often not the best way to compare them.
#
# ~~~
# assert 0.01.is_approx(0.02, 0.1) == true
# assert 0.01.is_approx(0.02, 0.001) == false
# ~~~
fun is_approx(other, precision: Float): Bool
do
assert precision >= 0.0
return self <= other + precision and self >= other - precision
end
redef fun max(other)
do
if self < other then
return other
else
return self
end
end
redef fun min(c)
do
if c < self then
return c
else
return self
end
end
end
lib/core/kernel.nit:517,1--599,3
redef class Float
# Returns the non-negative square root of `self`.
#
# assert 9.0.sqrt == 3.0
# #assert 3.0.sqrt == 1.732
# assert 1.0.sqrt == 1.0
# assert 0.0.sqrt == 0.0
fun sqrt: Float `{ return sqrt(self); `}
# Computes the cosine of `self` (expressed in radians).
#
# #assert pi.cos == -1.0
fun cos: Float `{ return cos(self); `}
# Computes the sine of `self` (expressed in radians).
#
# #assert pi.sin == 0.0
fun sin: Float `{ return sin(self); `}
# Computes the cosine of x (expressed in radians).
#
# #assert 0.0.tan == 0.0
fun tan: Float `{ return tan(self); `}
# Computes the arc cosine of `self`.
#
# #assert 0.0.acos == pi / 2.0
fun acos: Float `{ return acos(self); `}
# Computes the arc sine of `self`.
#
# #assert 1.0.asin == pi / 2.0
fun asin: Float `{ return asin(self); `}
# Computes the arc tangent of `self`.
#
# #assert 0.0.tan == 0.0
fun atan: Float `{ return atan(self); `}
# Returns the absolute value of `self`.
#
# assert 12.0.abs == 12.0
# assert (-34.56).abs == 34.56
# assert -34.56.abs == -34.56
fun abs: Float `{ return fabs(self); `}
# Returns `self` raised at `e` power.
#
# assert 2.0.pow(0.0) == 1.0
# assert 2.0.pow(3.0) == 8.0
# assert 0.0.pow(9.0) == 0.0
fun pow(e: Float): Float `{ return pow(self, e); `}
# Natural logarithm of `self`.
#
# assert 0.0.log.is_inf == -1
# assert 1.0.log == 0.0
fun log: Float `{ return log(self); `}
# Logarithm of `self` to base `base`.
#
# assert 100.0.log_base(10.0) == 2.0
# assert 256.0.log_base(2.0) == 8.0
fun log_base(base: Float): Float do return log/base.log
# Returns *e* raised to `self`.
fun exp: Float `{ return exp(self); `}
# assert 1.1.ceil == 2.0
# assert 1.9.ceil == 2.0
# assert 2.0.ceil == 2.0
# assert (-1.5).ceil == -1.0
fun ceil: Float `{ return ceil(self); `}
# assert 1.1.floor == 1.0
# assert 1.9.floor == 1.0
# assert 2.0.floor == 2.0
# assert (-1.5).floor == -2.0
fun floor: Float `{ return floor(self); `}
# Rounds the value of a float to its nearest integer value
#
# assert 1.67.round == 2.0
# assert 1.34.round == 1.0
# assert -1.34.round == -1.0
# assert -1.67.round == -2.0
fun round: Float `{ return round(self); `}
# Returns a random `Float` in `[0.0 .. self[`.
fun rand: Float `{
if (nit_rand_seeded) return ((self)*nit_rand())/(NIT_RAND_MAX+1.0);
return ((self)*rand())/(RAND_MAX+1.0);
`}
# Returns the euclidean distance from `b`.
fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
# Returns true is self is not a number.
#
# As `nan != nan`, `is_nan` should be used to test if a float is the special *not a number* value.
#
# ~~~
# assert nan != nan # By IEEE 754
# assert nan.is_nan
# assert not 10.0.is_nan
# ~~~
fun is_nan: Bool `{ return isnan(self); `}
# Is the float an infinite value
# this function returns:
#
# * 1 if self is positive infinity
# * -1 if self is negative infinity
# * 0 otherwise
#
# ~~~
# assert 10.0.is_inf == 0
# assert inf.is_inf == 1
# assert (-inf).is_inf == -1
# ~~~
fun is_inf: Int do
if native_is_inf then
if self < 0.0 then return -1
return 1
end
return 0
end
private fun native_is_inf: Bool `{ return isinf(self); `}
# Linear interpolation between `a` and `b` using `self` as weight
#
# ~~~
# assert 0.0.lerp(0.0, 128.0) == 0.0
# assert 0.5.lerp(0.0, 128.0) == 64.0
# assert 1.0.lerp(0.0, 128.0) == 128.0
# assert -0.5.lerp(0.0, 128.0) == -64.0
# ~~~
fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b
# Quadratic Bézier interpolation between `a` and `b` with an `handle` using `self` as weight
#
# ~~~
# assert 0.00.qerp(0.0, 32.0, 128.0) == 0.0
# assert 0.25.qerp(0.0, 32.0, 128.0) == 20.0
# assert 0.50.qerp(0.0, 32.0, 128.0) == 48.0
# assert 0.75.qerp(0.0, 32.0, 128.0) == 84.0
# assert 1.00.qerp(0.0, 32.0, 128.0) == 128.0
# ~~~
fun qerp(a, handle, b: Float): Float do
var p = self
var i = 1.0 - p
var r = i*i * a +
2.0*i*p * handle +
p*p * b
return r
end
# Cubic Bézier interpolation between `a` and `b` with two handles using `self` as weight
#
# The Cubic Bézier interpolation is the most common one and use two control points.
#
# ~~~
# assert 0.00.cerp(0.0, 32.0, 128.0, 64.0) == 0.0
# assert 0.25.cerp(0.0, 32.0, 128.0, 64.0) == 32.5
# assert 0.50.cerp(0.0, 32.0, 128.0, 64.0) == 68.0
# assert 0.75.cerp(0.0, 32.0, 128.0, 64.0) == 85.5
# assert 1.00.cerp(0.0, 32.0, 128.0, 64.0) == 64.0
# ~~~
fun cerp(a, a_handle, b_handle, b: Float): Float do
var p = self
var i = 1.0 - p
var r = i*i*i * a +
3.0*i*i*p * a_handle +
3.0*i*p*p * b_handle +
p*p*p * b
return r
end
end
lib/core/math.nit:206,1--385,3
redef class Float
# Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
#
# ~~~
# assert 12.34.to_s == "12.34"
# assert (-0120.030).to_s == "-120.03"
# assert (-inf).to_s == "-inf"
# assert (nan).to_s == "nan"
# ~~~
#
# see `to_precision` for a custom precision.
redef fun to_s do
var str = to_precision(3)
return adapt_number_of_decimal(str, false)
end
# Return the representation of `self`, with scientific notation
#
# Adpat the number of decimals as needed from 1 to a maximum of 6
# ~~~
# assert 12.34.to_sci == "1.234e+01"
# assert 123.45.to_sci.to_f.to_sci == "1.2345e+02"
# assert 0.001234.to_sci == "1.234e-03"
# assert (inf).to_sci == "inf"
# assert (nan).to_sci == "nan"
# ~~~
fun to_sci: String
do
var is_inf_or_nan = check_inf_or_nan
if is_inf_or_nan != null then return is_inf_or_nan
return adapt_number_of_decimal(return_from_specific_format("%e".to_cstring), true)
end
# Return the `string_number` with the adapted number of decimal (i.e the fonction remove the useless `0`)
# `is_expo` it's here to specifi if the given `string_number` is in scientific notation
private fun adapt_number_of_decimal(string_number: String, is_expo: Bool): String
do
# check if `self` does not need an adaptation of the decimal
if is_inf != 0 or is_nan then return string_number
var len = string_number.length
var expo_value = ""
var numeric_value = ""
for i in [0..len-1] do
var j = len - 1 - i
var c = string_number.chars[j]
if not is_expo then
if c == '0' then
continue
else if c == '.' then
numeric_value = string_number.substring( 0, j + 2)
break
else
numeric_value = string_number.substring( 0, j + 1)
break
end
else if c == 'e' then
expo_value = string_number.substring( j, len - 1 )
is_expo = false
end
end
return numeric_value + expo_value
end
# Return a string representation of `self` in fonction if it is not a number or infinity.
# Return `null` if `self` is not a not a number or an infinity
private fun check_inf_or_nan: nullable String
do
if is_nan then return "nan"
var isinf = self.is_inf
if isinf == 1 then
return "inf"
else if isinf == -1 then
return "-inf"
end
return null
end
# `String` representation of `self` with the given number of `decimals`
#
# ~~~
# assert 12.345.to_precision(0) == "12"
# assert 12.345.to_precision(3) == "12.345"
# assert (-12.345).to_precision(3) == "-12.345"
# assert (-0.123).to_precision(3) == "-0.123"
# assert 0.999.to_precision(2) == "1.00"
# assert 0.999.to_precision(4) == "0.9990"
# ~~~
fun to_precision(decimals: Int): String
do
var is_inf_or_nan = check_inf_or_nan
if is_inf_or_nan != null then return is_inf_or_nan
return return_from_specific_format("%.{decimals}f".to_cstring)
end
# Returns the hexadecimal (`String`) representation of `self` in exponential notation
#
# ~~~
# assert 12.345.to_hexa_exponential_notation == "0x1.8b0a3d70a3d71p+3"
# assert 12.345.to_hexa_exponential_notation.to_f == 12.345
# ~~~
fun to_hexa_exponential_notation: String
do
return return_from_specific_format("%a".to_cstring)
end
# Return the representation of `self`, with the specific given c `format`.
private fun return_from_specific_format(format: CString): String
do
var size = to_precision_size_with_format(format)
var cstr = new CString(size + 1)
to_precision_fill_with_format(format, size + 1, cstr)
return cstr.to_s_unsafe(byte_length = size, copy = false)
end
# The lenght of `self` in the specific given c `format`
private fun to_precision_size_with_format(format: CString): Int`{
return snprintf(NULL, 0, format, self);
`}
# Fill `cstr` with `self` in the specific given c `format`
private fun to_precision_fill_with_format(format: CString, size: Int, cstr: CString) `{
snprintf(cstr, size, format, self);
`}
end
lib/core/text/abstract_text.nit:2026,1--2150,3
redef class Float
# Utility for `BinaryWriter`
private fun float_byte_at(index: Int, big_endian: Bool): Int `{
union {
unsigned char bytes[4];
float val;
uint32_t conv;
} u;
u.val = self;
if (big_endian)
u.conv = htobe32(u.conv);
else u.conv = htole32(u.conv);
return u.bytes[index];
`}
# Utility for `BinaryWriter`
private fun double_byte_at(index: Int, big_endian: Bool): Int `{
union {
unsigned char bytes[8];
double val;
uint64_t conv;
} u;
u.val = self;
if (big_endian)
u.conv = htobe64(u.conv);
else u.conv = htole64(u.conv);
return u.bytes[index];
`}
end
lib/binary/binary.nit:361,1--395,3
redef universal Float super Sqlite3Data end
lib/sqlite3/sqlite3.nit:319,1--43
redef class Float super DirectSerializable end
lib/serialization/serialization_core.nit:262,1--46
redef universal Float
# Normalize the `self` angle in radians to be within `[-pi .. pi[`
#
# ~~~
# assert (1.5*pi).angle_normalize.is_approx(-0.5*pi, 0.0001)
# assert 8.0.angle_normalize.is_approx(1.7168, 0.0001)
# assert (-1.0).angle_normalize == -1.0
# ~~~
fun angle_normalize: Float
do
var s = self
while s < -pi do s += 2.0*pi
while s >= pi do s -= 2.0*pi
return s
end
# Linear interpolation of between the angles `a` and `b`, in radians
#
# The result is normalized with `angle_normalize`.
#
# ~~~
# assert 0.5.angle_lerp(0.0, pi).is_approx(0.5*pi, 0.0001)
# assert 8.5.angle_lerp(0.0, pi).is_approx(0.5*pi, 0.0001)
# assert 7.5.angle_lerp(0.0, pi).is_approx(-0.5*pi, 0.0001)
# assert 0.5.angle_lerp(0.2, 2.0*pi-0.1).is_approx(0.05, 0.0001)
# ~~~
fun angle_lerp(a, b: Float): Float
do
var d = b - a
while d > pi do d -= 2.0*pi
while d < -pi do d += 2.0*pi
return (a + d*self).angle_normalize
end
end
lib/geometry/angles.nit:38,1--71,3
redef class Float
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_double self
end
lib/msgpack/serialization_write.nit:252,1--254,3
redef class Float
redef fun add_to_bundle(bundle, key)
do
bundle.put_double(key, self)
end
end
lib/android/bundle/bundle.nit:700,1--705,3