float_utils.h
Include dependency graph for float_utils.h:
This graph shows which files directly or indirectly include float_utils.h:
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.”).
-
enumerator FloatFormatShort
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.