Skip to content

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)
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float small = FloatFrom((u64)5, &alloc.base);
        int   cmp   = FloatCompare(&small, (u64)100);
        bool  ok    = cmp < 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatFrom((u64)42, &alloc.base);
        int   cmp   = FloatCompare(&value, (u64)42);
        bool  ok    = cmp == 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float a = FloatFrom(5.0, &alloc.base);
        Float b = FloatFrom(0.0, &alloc.base);
    
        Float a = FloatFrom(5.0, &alloc.base);
        Float b = FloatFrom(0.0, &alloc.base);
    
        int  cmp    = FloatCompare(&a, &b);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float a = FloatFrom(0.0, &alloc.base);
        Float b = FloatFrom(5.0, &alloc.base);
    
        Float a = FloatFrom(0.0, &alloc.base);
        Float b = FloatFrom(5.0, &alloc.base);
    
        int  cmp    = FloatCompare(&a, &b);
    // 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();
    
        double two_pow_60 = 1152921504606846976.0; // 2^60, exact in f64
        Float  value      = FloatFrom(two_pow_60, &alloc.base);
        Str    text       = FloatToStr(&value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float neg = FloatFrom(-1.0f, &alloc.base);
        Float pos = FloatFrom(1.0f, &alloc.base);
    
        Float neg = FloatFrom(-1.0f, &alloc.base);
        Float pos = FloatFrom(1.0f, &alloc.base);
    
        bool result = FloatIsNegative(&neg) && !FloatIsNegative(&pos);
    
        // 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);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float a = FloatFrom(0x1p-149f, &alloc.base); // m == 1
        Float b = FloatFrom(0x3p-149f, &alloc.base); // m == 3
    
        Float a = FloatFrom(0x1p-149f, &alloc.base); // m == 1
        Float b = FloatFrom(0x3p-149f, &alloc.base); // m == 3
    
        bool result = FloatNE(&a, &b);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float neg = FloatFrom(-1.0, &alloc.base);
        Float pos = FloatFrom(1.0, &alloc.base);
    
        Float neg = FloatFrom(-1.0, &alloc.base);
        Float pos = FloatFrom(1.0, &alloc.base);
    
        bool result = FloatIsNegative(&neg) && !FloatIsNegative(&pos);
    
        // 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);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float a = FloatFrom(0x1p-1074, &alloc.base); // m == 1
        Float b = FloatFrom(0x3p-1074, &alloc.base); // m == 3
    
        Float a = FloatFrom(0x1p-1074, &alloc.base); // m == 1
        Float b = FloatFrom(0x3p-1074, &alloc.base); // m == 3
    
        bool result = FloatNE(&a, &b);
        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
    
    bool test_float_from_unsigned_integer(void) {
        WriteFmt("Testing FloatFrom with unsigned integer\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
    bool test_float_from_signed_integer(void) {
        WriteFmt("Testing FloatFrom with signed integer\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
    bool test_float_from_int_container(void) {
        WriteFmt("Testing FloatFrom with Int container\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Int   whole = IntFrom(100, &alloc.base);
        Float value = FloatFrom(&whole, &alloc.base);
    
        // 100 == 1 * 10^2 after normalization.
    
        Int   whole = IntFrom(100, &alloc.base);
        Float value = FloatFrom(&whole, &alloc.base);
        Str   text  = FloatToStr(&value);
    
        Int   whole = IntFrom(2000, &alloc.base);
        Float value = FloatFrom(&whole, &alloc.base);
    
        bool result = FloatExponent(&value) == 3;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatFrom((u64)100, &alloc.base);
        bool  ok    = FloatExponent(&value) == 2;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatFrom((u64)100, &alloc.base);
        Str   text  = FloatToStr(&value);
        bool  ok    = ZstrCompare(StrBegin(&text), "100") == 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatFrom((i64)-200, &alloc.base);
        bool  ok    = FloatExponent(&value) == 2;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatFrom(4503599627370496.0, &alloc.base); // 2^52, binexp == 0
        Int   whole = IntFromStr("4503599627370496", &alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Float value = FloatFrom(4503599627370496.0, &alloc.base);
        Str   text  = FloatToStr(&value);
        DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
    
        Float v = FloatFrom(0.5, &dbg.base); // binexp < 0 -> 5^|binexp| path
        FloatDeinit(&v);
        DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
    
        Float v = FloatFrom(1.5f, &dbg.base); // binexp < 0 -> 5^|binexp| path
        FloatDeinit(&v);
        DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
    
        Float v = FloatFrom(1000u, &dbg.base); // 1000 -> significand 1, exp 3
        FloatDeinit(&v);
        DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
    
        Float v = FloatFrom(0u, &dbg.base);
        Int   r = IntFrom(424242u, &dbg.base); // pre-populated dest
Last updated on