diff --git a/src/bigfloat.cpp b/src/bigfloat.cpp index 8e19f0a031..ba09945824 100644 --- a/src/bigfloat.cpp +++ b/src/bigfloat.cpp @@ -5,6 +5,11 @@ * See http://opensource.org/licenses/MIT */ +// TODO in this file we cast between __float128 and long double +// and lose precision. for now this is going to be a bug in +// the compiler because I don't want to add a dependency on libquadmath. +// when we self host we can use zig's f128 and the problem is fixed. + #include "bigfloat.hpp" #include "bigint.hpp" #include "buffer.hpp" @@ -46,7 +51,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_ char *str_begin = (char *)buf_ptr; char *str_end; errno = 0; - dest->value = strtold(str_begin, &str_end); + dest->value = (__float128)strtold(str_begin, &str_end); if (errno) { return ErrorOverflow; } @@ -77,22 +82,22 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { dest->value = op1->value / op2->value; if (dest->value >= 0.0) { - dest->value = floorl(dest->value); + dest->value = (__float128)floorl((long double)dest->value); } else { - dest->value = ceill(dest->value); + dest->value = (__float128)ceill((long double)dest->value); } } void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { - dest->value = floorl(op1->value / op2->value); + dest->value = (__float128)floorl((long double)(op1->value / op2->value)); } void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { - dest->value = fmodl(op1->value, op2->value); + dest->value = (__float128)fmodl((long double)op1->value, (long double)op2->value); } void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { - dest->value = fmodl(fmodl(op1->value, op2->value) + op2->value, op2->value); + dest->value = (__float128)fmodl(fmodl((long double)op1->value, (long double)op2->value) + (long double)op2->value, (long double)op2->value); } void bigfloat_write_buf(Buf *buf, const BigFloat *op) { @@ -159,5 +164,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) { } bool bigfloat_has_fraction(const BigFloat *bigfloat) { - return floorl(bigfloat->value) != bigfloat->value; + return ((__float128)floorl((long double)bigfloat->value)) != bigfloat->value; }