authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-18 13:13:03-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-18 13:13:03-04:00
logb73d4f74c2826b8a87215eaaa1f4e4bfa88be81b
tree95235e815dc239a8c61efbde3b83eff7cbef6942
parente1c225694df6b99b7541a99a59def52a5886c665

depend on libquadmath

it seems to be shipped with gcc and clang

2 files changed, 13 insertions(+), 13 deletions(-)

CMakeLists.txt+1
......@@ -123,6 +123,7 @@ target_link_libraries(zig LINK_PUBLIC
123123 ${LLD_LIBRARIES}
124124 ${LLVM_LIBRARIES}
125125 ${CMAKE_THREAD_LIBS_INIT}
126 quadmath
126127)
127128if(MINGW)
128129 target_link_libraries(zig LINK_PUBLIC version)
src/bigfloat.cpp+12-13
......@@ -5,16 +5,12 @@
55 * See http://opensource.org/licenses/MIT
66 */
77
8// TODO in this file we cast between __float128 and double
9// and lose precision. for now this is going to be a bug in
10// the compiler because I don't want to add a dependency on libquadmath.
11// when we self host we can use zig's f128 and the problem is fixed.
12
138#include "bigfloat.hpp"
149#include "bigint.hpp"
1510#include "buffer.hpp"
1611#include <math.h>
1712#include <errno.h>
13#include <quadmath.h>
1814
1915void bigfloat_init_float(BigFloat *dest, __float128 x) {
2016 dest->value = x;
......@@ -51,7 +47,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_
5147 char *str_begin = (char *)buf_ptr;
5248 char *str_end;
5349 errno = 0;
54 dest->value = (__float128)strtod(str_begin, &str_end);
50 dest->value = strtoflt128(str_begin, &str_end);
5551 if (errno) {
5652 return ErrorOverflow;
5753 }
......@@ -82,26 +78,29 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
8278void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
8379 dest->value = op1->value / op2->value;
8480 if (dest->value >= 0.0) {
85 dest->value = (__float128)floor((double)dest->value);
81 dest->value = floorq(dest->value);
8682 } else {
87 dest->value = (__float128)ceil((double)dest->value);
83 dest->value = ceilq(dest->value);
8884 }
8985}
9086
9187void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
92 dest->value = (__float128)floor((double)(op1->value / op2->value));
88 dest->value = floorq(op1->value / op2->value);
9389}
9490
9591void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
96 dest->value = (__float128)fmod((double)op1->value, (double)op2->value);
92 dest->value = fmodq(op1->value, op2->value);
9793}
9894
9995void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
100 dest->value = (__float128)fmod(fmod((double)op1->value, (double)op2->value) + (double)op2->value, (double)op2->value);
96 dest->value = fmodq(fmodq(op1->value, op2->value) + op2->value, op2->value);
10197}
10298
10399void bigfloat_write_buf(Buf *buf, const BigFloat *op) {
104 buf_appendf(buf, "%f", (double)op->value);
100 buf_resize(buf, 256);
101 int len = quadmath_snprintf(buf_ptr(buf), buf_len(buf), "%Qf", op->value);
102 assert(len > 0);
103 buf_resize(buf, len);
105104}
106105
107106Cmp bigfloat_cmp(const BigFloat *op1, const BigFloat *op2) {
......@@ -164,5 +163,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {
164163}
165164
166165bool bigfloat_has_fraction(const BigFloat *bigfloat) {
167 return ((__float128)floor((double)bigfloat->value)) != bigfloat->value;
166 return floorq(bigfloat->value) != bigfloat->value;
168167}