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);
    
    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);
Last updated on