diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-08-18 13:13:03 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-08-18 13:13:03 -0400 |
| commit | b73d4f74c2826b8a87215eaaa1f4e4bfa88be81b (patch) | |
| tree | 95235e815dc239a8c61efbde3b83eff7cbef6942 /src/bigfloat.cpp | |
| parent | e1c225694df6b99b7541a99a59def52a5886c665 (diff) | |
| download | zig-b73d4f74c2826b8a87215eaaa1f4e4bfa88be81b.tar.gz zig-b73d4f74c2826b8a87215eaaa1f4e4bfa88be81b.zip | |
depend on libquadmath
it seems to be shipped with gcc and clang
Diffstat (limited to 'src/bigfloat.cpp')
| -rw-r--r-- | src/bigfloat.cpp | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/bigfloat.cpp b/src/bigfloat.cpp index 9db45c6e13..1d24a50a3e 100644 --- a/src/bigfloat.cpp +++ b/src/bigfloat.cpp @@ -5,16 +5,12 @@ * See http://opensource.org/licenses/MIT */ -// TODO in this file we cast between __float128 and 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" #include <math.h> #include <errno.h> +#include <quadmath.h> void bigfloat_init_float(BigFloat *dest, __float128 x) { dest->value = x; @@ -51,7 +47,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 = (__float128)strtod(str_begin, &str_end); + dest->value = strtoflt128(str_begin, &str_end); if (errno) { return ErrorOverflow; } @@ -82,26 +78,29 @@ 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 = (__float128)floor((double)dest->value); + dest->value = floorq(dest->value); } else { - dest->value = (__float128)ceil((double)dest->value); + dest->value = ceilq(dest->value); } } void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { - dest->value = (__float128)floor((double)(op1->value / op2->value)); + dest->value = floorq(op1->value / op2->value); } void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { - dest->value = (__float128)fmod((double)op1->value, (double)op2->value); + dest->value = fmodq(op1->value, op2->value); } void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { - dest->value = (__float128)fmod(fmod((double)op1->value, (double)op2->value) + (double)op2->value, (double)op2->value); + dest->value = fmodq(fmodq(op1->value, op2->value) + op2->value, op2->value); } void bigfloat_write_buf(Buf *buf, const BigFloat *op) { - buf_appendf(buf, "%f", (double)op->value); + buf_resize(buf, 256); + int len = quadmath_snprintf(buf_ptr(buf), buf_len(buf), "%Qf", op->value); + assert(len > 0); + buf_resize(buf, len); } Cmp bigfloat_cmp(const BigFloat *op1, const BigFloat *op2) { @@ -164,5 +163,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) { } bool bigfloat_has_fraction(const BigFloat *bigfloat) { - return ((__float128)floor((double)bigfloat->value)) != bigfloat->value; + return floorq(bigfloat->value) != bigfloat->value; } |
