Float

enum Float

Native floating point numbers.

Corresponds to C float.

Inheritance

dep_class_core__kernel__FloatObjectObjectSerializableSerializableSerializable->ObjectComparableComparableComparable->ObjectSqlite3DataSqlite3DataSqlite3Data->ObjectPepperablePepperablePepperable->ObjectDirectSerializableDirectSerializableDirectSerializable->SerializableNumericNumericNumeric->ComparableFloatFloatFloat->Sqlite3DataFloat->PepperableFloat->DirectSerializableFloat->Numeric

Parents

  • DirectSerializable: Instances of this class are not delayed and instead serialized immediately
  • Numeric: A numeric value supporting mathematical operations
  • Pepperable: Provides a method to convert in PepperVars.
  • Sqlite3Data: A data type supported by Sqlite3

Ancestors

  • Comparable: The ancestor of class where objects are in a total order.
  • Object: The root of the class hierarchy.
  • Serializable: Instances of this class can be passed to Serializer::serialize

Constructors

Concerns

  • core: Nit common library of core classes and methods
    • kernel: Most basic classes and methods.
    • fixed_ints: Basic integers of fixed-precision
    • math: Mathematical operations
    • numeric: Advanced services for Numeric types
    • text: All the classes and methods related to the manipulation of text entities
      • abstract_text: Abstract class for manipulation of sequences of characters
    • time: Management of time and dates
  • jvm: Java Virtual Machine invocation API and others services from the JNI C API
    • jvm: Java Virtual Machine invocation API and others services from the JNI C API
  • gmp: Multi precision integer and rational number using gmp lib
    • gmp: Multi precision integer and rational number using gmp lib
  • pnacl: Provides PNaCl support for Nit.
    • pnacl: Provides PNaCl support for Nit.
  • noise: Noise generators PerlinNoise and InterpolatedNoise
    • noise: Noise generators PerlinNoise and InterpolatedNoise
  • geometry: Basic geometry data structures and services.
    • angles: Angle related service using Float to represent an angle in radians
  • json: read and write JSON formatted text
    • serialization_write: Services to write Nit objects to JSON strings: serialize_to_json and JsonSerializer
  • msgpack: MessagePack, an efficient binary serialization format
  • android: Android platform support and APIs
    • bundle: A mapping class of String to various value types used by the
      • bundle: A mapping class of String to various value types used by the

in core::kernel

redef type SELF

Type of this instance, automatically specialized in every class

A common use case of the virtual type SELF is to type an attribute and store another instance of the same type as self. It can also be used as as return type to a method producing a copy of self or returning an instance expected to be the exact same type as self.

This virtual type must be used with caution as it can hinder specialization. In fact, it imposes strict restrictions on all sub-classes and their usage. For example, using SELF as a return type of a method foo forces all subclasses to ensure that foo returns the correct and updated type. A dangerous usage take the form of a method typed by SELF which creates and returns a new instance. If not correctly specialized, this method would break when invoked on a sub-class.

A general rule for safe usage of SELF is to ensure that inputs typed SELF are stored in attributes typed SELF and returned by methods typed SELF, pretty much the same things as you would do with parameter types.

redef intern fun ==(i)

Have self and other the same value?

assert 1 + 1 == 2
assert not 1 == "1"
assert 1.to_s == "1"

The exact meaning of same value is left to the subclasses. Implicitly, the default implementation, is is_same_instance.

The laws of == are the following:

  • reflexivity a.is_same_instance(b) implies a == b
  • symmetry: (a == b) == (b == a)
  • transitivity: (a == b) and (b == c) implies (a == c)

== might not be constant on some objects overtime because of their evolution.

var a = [1]
var b = [1]
var c = [1,2]
assert a == b and not a == c
a.add 2
assert not a == b and a == c

Lastly, == is highly linked with hash and a specific redefinition of == should usually be associated with a specific redefinition of hash.

ENSURE result implies self.hash == other.hash

fun is_approx(other: Float, precision: Float): Bool

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

redef intern fun object_id

An internal hash code for the object based on its identity.

Unless specific code, you should not use this method but use hash instead.

As its name hints it, the internal hash code, is used internally to provide a hash value. It is also used by the inspect method to loosely identify objects and helps debugging.

var a = "Hello"
var b = a
assert a.object_id == b.object_id

The specific details of the internal hash code it let to the specific engine. The rules are the following:

  • The object_id MUST be invariant for the whole life of the object.
  • Two living instances of the same classes SHOULD NOT share the same object_id.
  • Two instances of different classes MIGHT share the same object_id.
  • The object_id of a garbage-collected instance MIGHT be reused by new instances.
  • The object_id of an object MIGHT be non constant across different executions.

For instance, the nitc compiler uses the address of the object in memory as its object_id.

TODO rename in something like internal_hash_code

in core::fixed_ints

in core::math

fun cerp(a: Float, a_handle: Float, b_handle: Float, b: Float): Float

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 is_inf: Int

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_nan: Bool

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 lerp(a: Float, b: Float): Float

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 qerp(a: Float, handle: Float, b: Float): Float

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

in core::numeric

in core::text::abstract_text

fun to_precision(decimals: Int): String

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"

in core::time

in jvm::jvm

in gmp::gmp

in pnacl::pnacl

in noise::noise

redef fun core_serialize_to(v)

Actual serialization of self to serializer

This writes the full data of self to serializer.

This method can be redefined in sub classes and refinements. It should use Serializer::serialize_attribute to to register real or logical attributes.

Any refinement should have its equivalent refinement of Deserializer::deserialize_class to support this custom deserialization.

in geometry::angles

fun angle_lerp(a: Float, b: Float): Float

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)

in json::serialization_write

redef fun accept_json_serializer(v)

Refinable service to customize the serialization of this class to JSON

This method can be refined to customize the serialization by either writing pure JSON directly on the stream v.stream or by using other services of JsonSerializer.

Most of the time, it is preferable to refine the method core_serialize_to which is used by all the serialization engines, not just JSON.

in msgpack::serialization_write

redef fun accept_msgpack_serializer(v)

Hook to customize the serialization of this class to MessagePack

This method can be refined to customize the serialization by either writing pure JSON directly on the stream v.stream or by using other services of MsgPackSerializer.

Most of the time, it is better to refine the method core_serialize_to which is used by all the serialization engines, not just MessagePack.

in android::bundle::bundle

redef fun add_to_bundle(bundle, key)

Called by []= to dynamically choose the appropriate method according

to the value type to store Non-primitive Object (String excluded) will be stored as a serialized json String Refine your class to customize this method behaviour