FloatToInt
FloatToInt
Description
Convert a float to an integer when no fractional or negative part remains.
Parameters
| Name | Direction | Description |
|---|---|---|
result |
out | Destination integer |
value |
in | Float to convert |
Usage example (from documentation)
bool ok = FloatToInt(&integer, &value);Returns
true on exact non-negative conversion, otherwise false.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Float.c:216:
}
bool FloatToInt(Int *result, Float *value) {
Int temp = IntInit();
bool test_float_to_int_exact(void) {
WriteFmt("Testing FloatToInt exact conversion\n");
Float value = FloatFromStr("1234500e-2"); Str text = StrInit();
bool result = FloatToInt(&result_value, &value);
text = IntToStr(&result_value);
result = result && (strcmp(text.data, "12345") == 0);
bool test_float_to_int_fractional_failure(void) {
WriteFmt("Testing FloatToInt fractional failure handling\n");
Float value = FloatFromStr("123.45"); Int result_value = IntFrom(99);
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");
Float value = FloatFromStr("-42"); Int result_value = IntFrom(99);
bool result = !FloatToInt(&result_value, &value);
result = result && IntEQ(&result_value, 99);
Last updated on