float_utils.h

Include dependency graph for float_utils.h:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "1" [label="/__w/AtomVM/AtomVM/src/libAtomVM/float_utils.h" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/float_utils.h" fillcolor="#BFBFBF"] "4" [label="term_typedef.h" tooltip="term_typedef.h"] "5" [label="assert.h" tooltip="assert.h"] "7" [label="inttypes.h" tooltip="inttypes.h"] "6" [label="limits.h" tooltip="limits.h"] "2" [label="stddef.h" tooltip="stddef.h"] "3" [label="stdint.h" tooltip="stdint.h"] "1" -> "2" [dir=forward tooltip="include"] "1" -> "3" [dir=forward tooltip="include"] "1" -> "4" [dir=forward tooltip="include"] "4" -> "5" [dir=forward tooltip="include"] "4" -> "6" [dir=forward tooltip="include"] "4" -> "7" [dir=forward tooltip="include"] "4" -> "3" [dir=forward tooltip="include"] }

This graph shows which files directly or indirectly include float_utils.h:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "2" [label="/__w/AtomVM/AtomVM/src/libAtomVM/float_utils.c" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/float_utils.c"] "1" [label="/__w/AtomVM/AtomVM/src/libAtomVM/float_utils.h" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/float_utils.h" fillcolor="#BFBFBF"] "3" [label="/__w/AtomVM/AtomVM/src/libAtomVM/nifs.c" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/nifs.c"] "1" -> "2" [dir=back tooltip="include"] "1" -> "3" [dir=back tooltip="include"] }

Floating-point to ASCII conversion.

Defines

FLOAT_UTILS_ENABLE_DOUBLE_API
DOUBLE_WRITE_TO_ASCII_BUF_LEN 256

Maximum buffer size for double_write_to_ascii_buf / float_write_to_ascii_buf.

Enums

enum float_format_t

Output format for double_write_to_ascii_buf / float_write_to_ascii_buf.

Values:

enumerator FloatFormatShort

Shortest roundtrip representation (Grisu3 for doubles, best-effort for floats). Within (-2^53, 2^53): whichever notation is shorter. Outside that range: always scientific notation. Precision parameter is ignored.

enumerator FloatFormatScientific

Scientific notation (e.g. “3.14000e+00”). Precision = number of digits after the decimal point.

enumerator FloatFormatDecimals

Fixed-point decimal (e.g. “3.14000”). Precision = max digits after the decimal point.

enumerator FloatFormatDecimalsCompact

Fixed-point decimal with trailing zeros stripped (e.g. “3.14”). Like FloatFormatDecimals but trailing zeros after the decimal point are removed, keeping at least one fractional digit (e.g. “3.0” not “3.”).

Functions

int double_write_to_ascii_buf(double value, float_format_t format, int precision, char *buf)

Convert a finite double to an ASCII string.

Caller must ensure isfinite(value) is true. Output always uses ‘.’ as decimal separator regardless of locale.

buf must be at least DOUBLE_WRITE_TO_ASCII_BUF_LEN bytes.

Parameters:
  • value – the double to convert

  • format – output format

  • precision – digits after decimal point (ignored for FloatFormatShort)

  • buf – output buffer (at least DOUBLE_WRITE_TO_ASCII_BUF_LEN bytes)

Returns:

number of characters written (excluding null terminator), or -1 if the output was truncated

static inline int avm_float_write_to_ascii_buf(avm_float_t value, float_format_t format, int precision, char *buf)

Convert a finite avm_float_t to an ASCII string.

Dispatches to double_write_to_ascii_buf or float_write_to_ascii_buf depending on AVM_USE_SINGLE_PRECISION.