From 8544c4099ca8747486410f33cb1cb0c06ba2f684 Mon Sep 17 00:00:00 2001 From: Simon Johansson Date: Mon, 29 Jan 2024 08:26:04 +0100 Subject: Explain runtime differences for floats --- .../chapter0_basics/lesson05_floats/code.gleam | 3 +++ .../chapter0_basics/lesson05_floats/text.html | 24 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/content/chapter0_basics/lesson05_floats/code.gleam b/src/content/chapter0_basics/lesson05_floats/code.gleam index 8c4e89a..4241ab4 100644 --- a/src/content/chapter0_basics/lesson05_floats/code.gleam +++ b/src/content/chapter0_basics/lesson05_floats/code.gleam @@ -18,6 +18,9 @@ pub fn main() { io.debug(1.1 == 1.1) io.debug(2.1 == 1.2) + // Division by zero is not an error + io.debug(3.14 /. 0.0) + // Standard library float functions io.debug(float.max(2.0, 9.5)) io.debug(float.ceiling(5.4)) diff --git a/src/content/chapter0_basics/lesson05_floats/text.html b/src/content/chapter0_basics/lesson05_floats/text.html index cc993c4..1b56d73 100644 --- a/src/content/chapter0_basics/lesson05_floats/text.html +++ b/src/content/chapter0_basics/lesson05_floats/text.html @@ -1,15 +1,27 @@

Gleam's Float type represents numbers that are not integers.

- Unlike many languages Gleam does not have a NaN or - Infinity float value. + Gleam's numerical operators are not overloaded, so there are dedicatated + operators for working with floats.

- Gleam's numerical operators are not overloaded, so there are dedictated - operators for working with floats. + Floats are represented as 64 bit floating point numbers on both the Erlang and + JavaScript runtimes. The floating point behaviour is native to their + respective runtimes, so their exact behaviour will be slightly different + on the two runtimes. +

+

+ Under the JavaScript runtime, exceeding the maximum (or minimum) representable + value for a floating point value will result in Infinity (or + -Infinity). Should you try to divide two infinities you will + get NaN as a result. +

+

+ When running on the BEAM any overflow will raise an error. So there is + no NaN or Infinity float value in the Erlang + runtime.

- Floats are represented as 64 bit floating point numbers on both Erlang and - JavaScript runtimes. + Division by zero will not overflow, but is instead defined to be zero.

The -- cgit v1.2.3