diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-08-18 12:37:01 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-08-18 12:37:01 -0400 |
| commit | 3a762e5a8d7fbea9fad5553d11558e47b44e872a (patch) | |
| tree | 5ba28f0e5d40b1dd27654a3c861bfb32f2f292dc /src/bigfloat.cpp | |
| parent | 1b5d61bee9a50a6dc0941c3512ffa7e0abbf9c52 (diff) | |
| download | zig-3a762e5a8d7fbea9fad5553d11558e47b44e872a.tar.gz zig-3a762e5a8d7fbea9fad5553d11558e47b44e872a.zip | |
make casting between __float128 and long double explicit
Diffstat (limited to 'src/bigfloat.cpp')
| -rw-r--r-- | src/bigfloat.cpp | 19 |
1 files changed, 12 insertions, 7 deletions
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; } |
