Skip to content

FloatToInt

Description

Convert a float to an arbitrary-precision integer (truncation toward zero - the fractional portion is discarded).

Parameters

Name Direction Description
result out Destination integer.
value in Float to convert.

Success

Returns true. *result holds the integer part of value (sign preserved). The source float is unchanged.

Failure

Returns false on allocation failure during the magnitude transfer. *result is left in a valid but unspecified state.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool FloatToInt(Int *result, const Float *value) {
        ValidateInt(result);
        ValidateFloat(value);
    // unchanged. This probe keeps passing under the emulated removal.
    bool test_ff_probe_to_int_negative(void) {
        WriteFmt("Probe: FloatToInt returns false for negative input\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   out = IntInit(&alloc.base);
    
        bool ok = (FloatToInt(&out, &neg) == false);
    
        IntDeinit(&out);
    // are both caller-observable, so the mutation is distinguished.
    bool test_m2_to_int_neg_exponent_exact(void) {
        WriteFmt("Testing FloatToInt on un-normalized 50e-1 (== 5)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result = IntInit(&alloc.base);
    
        bool ok = FloatToInt(&result, &value);
    
        Str  text  = IntToStr(&result);
    // Exercises the non-negative-exponent branch: 12 x 10^1 == 120.
    bool test_m2_to_int_integer_branch(void) {
        WriteFmt("Testing FloatToInt on 120 (exponent branch)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   result = IntInit(&alloc.base);
    
        bool ok = FloatToInt(&result, &value);
    
        Str  text  = IntToStr(&result);
    // Exercises the zero branch: result must be 0 and the call must succeed.
    bool test_m2_to_int_zero(void) {
        WriteFmt("Testing FloatToInt on 0\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   result = IntFromStr("999", &alloc.base);
    
        bool ok    = FloatToInt(&result, &value);
        bool right = ok && IntIsZero(&result);
    // Exercises the negative branch: FloatToInt rejects negative inputs.
    bool test_m2_to_int_negative_rejected(void) {
        WriteFmt("Testing FloatToInt rejects -7\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   result = IntInit(&alloc.base);
    
        bool ok    = FloatToInt(&result, &value);
        bool right = (ok == false);
    
    bool test_float_to_int_exact(void) {
        WriteFmt("Testing FloatToInt exact conversion\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(ALLOCATOR_OF(&alloc));
    
        bool result = FloatToInt(&result_value, &value);
        text        = IntToStr(&result_value);
        result      = result && (ZstrCompare(StrBegin(&text), "12345") == 0);
    
    bool test_float_to_int_fractional_failure(void) {
        WriteFmt("Testing FloatToInt fractional failure handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   result_value = IntFrom(99, ALLOCATOR_OF(&alloc));
    
        bool result = !FloatToInt(&result_value, &value);
        result      = result && IntEQ(&result_value, 99);
    
    bool test_float_to_int_negative_failure(void) {
        WriteFmt("Testing FloatToInt negative failure handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   result_value = IntFrom(99, ALLOCATOR_OF(&alloc));
    
        bool result = !FloatToInt(&result_value, &value);
        result      = result && IntEQ(&result_value, 99);
        Int   r = IntInit(&dbg.base);
    
        bool ok = FloatToInt(&r, &v);             // drives float_pow10 + success path
    
        FloatDeinit(&v);
        Int   r = IntFrom(999999999u, &dbg.base);  // pre-populated dest
    
        bool ok = FloatToInt(&r, &v);
    
        FloatDeinit(&v);
        Int   r = IntFrom(777u, &dbg.base);          // pre-populated dest
    
        bool ok = FloatToInt(&r, &v);
    
        FloatDeinit(&v);
        // Non-integer -> FloatToInt returns false, but the negative-exponent block
        // is still entered and `factor` is allocated then freed at [448].
        (void)FloatToInt(&r, &v);
    
        FloatDeinit(&v);
        Int   r = IntFrom(424242u, &dbg.base); // pre-populated dest
    
        bool ok = FloatToInt(&r, &v);
    
        FloatDeinit(&v);
Last updated on