FloatFrom
Description
Convert a numeric value into an arbitrary-precision float. Dispatches on the type of value. The allocator argument may be a typed allocator pointer (&heap) or a raw Allocator *.
Parameters
| Name | Direction | Description |
|---|---|---|
value |
in | Integer, Int, float, or double source value |
Usage example (from documentation)
DefaultAllocator a = DefaultAllocatorInit();
Float value = FloatFrom(42, &a);Success
Returns Float representing the same numeric value.
Failure
Returns an empty (zero) Float bound to the given allocator when the underlying conversion cannot complete (allocation failure on the significand, or an unsupported non-finite native-float input). The caller cannot distinguish that from a true-zero source without a prior IntTry* / FloatIsZero check; use the *Try* form when detection matters.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Compare.c:252:
DefaultAllocator alloc = DefaultAllocatorInit();
Float small = FloatFrom((u64)5, &alloc.base);
int cmp = FloatCompare(&small, (u64)100);
bool ok = cmp < 0;- In
Compare.c:266:
DefaultAllocator alloc = DefaultAllocatorInit();
Float value = FloatFrom((u64)42, &alloc.base);
int cmp = FloatCompare(&value, (u64)42);
bool ok = cmp == 0;- In
Compare.c:321:
DefaultAllocator alloc = DefaultAllocatorInit();
Float a = FloatFrom(5.0, &alloc.base);
Float b = FloatFrom(0.0, &alloc.base);- In
Compare.c:322:
Float a = FloatFrom(5.0, &alloc.base);
Float b = FloatFrom(0.0, &alloc.base);
int cmp = FloatCompare(&a, &b);- In
Compare.c:342:
DefaultAllocator alloc = DefaultAllocatorInit();
Float a = FloatFrom(0.0, &alloc.base);
Float b = FloatFrom(5.0, &alloc.base);- In
Compare.c:343:
Float a = FloatFrom(0.0, &alloc.base);
Float b = FloatFrom(5.0, &alloc.base);
int cmp = FloatCompare(&a, &b);- In
Type.c:329:
// printed string.
bool test_m5_from_double_large_integer_exponent_zero(void) {
WriteFmt("Testing FloatFrom(double) of a large integer keeps exponent 0\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Type.c:334:
double two_pow_60 = 1152921504606846976.0; // 2^60, exact in f64
Float value = FloatFrom(two_pow_60, &alloc.base);
Str text = FloatToStr(&value);- In
Math.c:687:
DefaultAllocator alloc = DefaultAllocatorInit();
Float neg = FloatFrom(-1.0f, &alloc.base);
Float pos = FloatFrom(1.0f, &alloc.base);- In
Math.c:688:
Float neg = FloatFrom(-1.0f, &alloc.base);
Float pos = FloatFrom(1.0f, &alloc.base);
bool result = FloatIsNegative(&neg) && !FloatIsNegative(&pos);- In
Math.c:709:
// 0x1p-149f: e == 0, m == 1 -> smallest positive f32 subnormal.
Float tiny = FloatFrom(0x1p-149f, &alloc.base);
bool result = FloatLT(&tiny, 1.0f) && FloatGT(&tiny, 0.0f);- In
Math.c:727:
DefaultAllocator alloc = DefaultAllocatorInit();
Float a = FloatFrom(0x1p-149f, &alloc.base); // m == 1
Float b = FloatFrom(0x3p-149f, &alloc.base); // m == 3
- In
Math.c:728:
Float a = FloatFrom(0x1p-149f, &alloc.base); // m == 1
Float b = FloatFrom(0x3p-149f, &alloc.base); // m == 3
bool result = FloatNE(&a, &b);- In
Math.c:745:
DefaultAllocator alloc = DefaultAllocatorInit();
Float neg = FloatFrom(-1.0, &alloc.base);
Float pos = FloatFrom(1.0, &alloc.base);- In
Math.c:746:
Float neg = FloatFrom(-1.0, &alloc.base);
Float pos = FloatFrom(1.0, &alloc.base);
bool result = FloatIsNegative(&neg) && !FloatIsNegative(&pos);- In
Math.c:765:
// 0x1p-1074: e == 0, m == 1 -> smallest positive f64 subnormal.
Float tiny = FloatFrom(0x1p-1074, &alloc.base);
bool result = FloatLT(&tiny, 1.0) && FloatGT(&tiny, 0.0);- In
Math.c:782:
DefaultAllocator alloc = DefaultAllocatorInit();
Float a = FloatFrom(0x1p-1074, &alloc.base); // m == 1
Float b = FloatFrom(0x3p-1074, &alloc.base); // m == 3
- In
Math.c:783:
Float a = FloatFrom(0x1p-1074, &alloc.base); // m == 1
Float b = FloatFrom(0x3p-1074, &alloc.base); // m == 3
bool result = FloatNE(&a, &b);- In
Math.c:1090:
DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
Float a = FloatFrom(0u, &dbg.base);
Float b = FloatFromStr("4", &dbg.base);
Float r = FloatFromStr("55555", &dbg.base); // pre-populated dest
- In
Convert.c:49:
bool test_float_from_unsigned_integer(void) {
WriteFmt("Testing FloatFrom with unsigned integer\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:66:
bool test_float_from_signed_integer(void) {
WriteFmt("Testing FloatFrom with signed integer\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:83:
bool test_float_from_int_container(void) {
WriteFmt("Testing FloatFrom with Int container\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:311:
Int whole = IntFrom(100, &alloc.base);
Float value = FloatFrom(&whole, &alloc.base);
// 100 == 1 * 10^2 after normalization.
- In
Convert.c:331:
Int whole = IntFrom(100, &alloc.base);
Float value = FloatFrom(&whole, &alloc.base);
Str text = FloatToStr(&value);- In
Convert.c:353:
Int whole = IntFrom(2000, &alloc.base);
Float value = FloatFrom(&whole, &alloc.base);
bool result = FloatExponent(&value) == 3;- In
Convert.c:377:
DefaultAllocator alloc = DefaultAllocatorInit();
Float value = FloatFrom((u64)100, &alloc.base);
bool ok = FloatExponent(&value) == 2;- In
Convert.c:389:
DefaultAllocator alloc = DefaultAllocatorInit();
Float value = FloatFrom((u64)100, &alloc.base);
Str text = FloatToStr(&value);
bool ok = ZstrCompare(StrBegin(&text), "100") == 0;- In
Convert.c:405:
DefaultAllocator alloc = DefaultAllocatorInit();
Float value = FloatFrom((i64)-200, &alloc.base);
bool ok = FloatExponent(&value) == 2;- In
Convert.c:627:
DefaultAllocator alloc = DefaultAllocatorInit();
Float value = FloatFrom(4503599627370496.0, &alloc.base); // 2^52, binexp == 0
Int whole = IntFromStr("4503599627370496", &alloc.base);- In
Convert.c:646:
DefaultAllocator alloc = DefaultAllocatorInit();
Float value = FloatFrom(4503599627370496.0, &alloc.base);
Str text = FloatToStr(&value);- In
Convert.c:668:
DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
Float v = FloatFrom(0.5, &dbg.base); // binexp < 0 -> 5^|binexp| path
FloatDeinit(&v);- In
Convert.c:679:
DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
Float v = FloatFrom(1.5f, &dbg.base); // binexp < 0 -> 5^|binexp| path
FloatDeinit(&v);- In
Convert.c:696:
DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
Float v = FloatFrom(1000u, &dbg.base); // 1000 -> significand 1, exp 3
FloatDeinit(&v);- In
Convert.c:805:
DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
Float v = FloatFrom(0u, &dbg.base);
Int r = IntFrom(424242u, &dbg.base); // pre-populated dest
Last updated on