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)
- In
Float.c:433:
}
bool FloatToInt(Int *result, const Float *value) {
ValidateInt(result);
ValidateFloat(value);- In
Type.c:170:
// 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();- In
Type.c:177:
Int out = IntInit(&alloc.base);
bool ok = (FloatToInt(&out, &neg) == false);
IntDeinit(&out);- In
Type.c:198:
// 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();- In
Type.c:209:
Int result = IntInit(&alloc.base);
bool ok = FloatToInt(&result, &value);
Str text = IntToStr(&result);- In
Type.c:223:
// 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();- In
Type.c:230:
Int result = IntInit(&alloc.base);
bool ok = FloatToInt(&result, &value);
Str text = IntToStr(&result);- In
Type.c:244:
// 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();- In
Type.c:251:
Int result = IntFromStr("999", &alloc.base);
bool ok = FloatToInt(&result, &value);
bool right = ok && IntIsZero(&result);- In
Type.c:262:
// 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();- In
Type.c:269:
Int result = IntInit(&alloc.base);
bool ok = FloatToInt(&result, &value);
bool right = (ok == false);- In
Convert.c:101:
bool test_float_to_int_exact(void) {
WriteFmt("Testing FloatToInt exact conversion\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:109:
Str text = StrInit(ALLOCATOR_OF(&alloc));
bool result = FloatToInt(&result_value, &value);
text = IntToStr(&result_value);
result = result && (ZstrCompare(StrBegin(&text), "12345") == 0);- In
Convert.c:121:
bool test_float_to_int_fractional_failure(void) {
WriteFmt("Testing FloatToInt fractional failure handling\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:128:
Int result_value = IntFrom(99, ALLOCATOR_OF(&alloc));
bool result = !FloatToInt(&result_value, &value);
result = result && IntEQ(&result_value, 99);- In
Convert.c:138:
bool test_float_to_int_negative_failure(void) {
WriteFmt("Testing FloatToInt negative failure handling\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:145:
Int result_value = IntFrom(99, ALLOCATOR_OF(&alloc));
bool result = !FloatToInt(&result_value, &value);
result = result && IntEQ(&result_value, 99);- In
Convert.c:716:
Int r = IntInit(&dbg.base);
bool ok = FloatToInt(&r, &v); // drives float_pow10 + success path
FloatDeinit(&v);- In
Convert.c:738:
Int r = IntFrom(999999999u, &dbg.base); // pre-populated dest
bool ok = FloatToInt(&r, &v);
FloatDeinit(&v);- In
Convert.c:760:
Int r = IntFrom(777u, &dbg.base); // pre-populated dest
bool ok = FloatToInt(&r, &v);
FloatDeinit(&v);- In
Convert.c:788:
// 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);- In
Convert.c:808:
Int r = IntFrom(424242u, &dbg.base); // pre-populated dest
bool ok = FloatToInt(&r, &v);
FloatDeinit(&v);
Last updated on