authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-18 12:37:01-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-18 12:37:01-04:00
log3a762e5a8d7fbea9fad5553d11558e47b44e872a
tree5ba28f0e5d40b1dd27654a3c861bfb32f2f292dc
parent1b5d61bee9a50a6dc0941c3512ffa7e0abbf9c52

make casting between __float128 and long double explicit


1 files changed, 12 insertions(+), 7 deletions(-)

src/bigfloat.cpp+12-7
......@@ -5,6 +5,11 @@
55 * See http://opensource.org/licenses/MIT
66 */
77
8// TODO in this file we cast between __float128 and long 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
813#include "bigfloat.hpp"
914#include "bigint.hpp"
1015#include "buffer.hpp"
......@@ -46,7 +51,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_
4651 char *str_begin = (char *)buf_ptr;
4752 char *str_end;
4853 errno = 0;
49 dest->value = strtold(str_begin, &str_end);
54 dest->value = (__float128)strtold(str_begin, &str_end);
5055 if (errno) {
5156 return ErrorOverflow;
5257 }
......@@ -77,22 +82,22 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
7782void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
7883 dest->value = op1->value / op2->value;
7984 if (dest->value >= 0.0) {
80 dest->value = floorl(dest->value);
85 dest->value = (__float128)floorl((long double)dest->value);
8186 } else {
82 dest->value = ceill(dest->value);
87 dest->value = (__float128)ceill((long double)dest->value);
8388 }
8489}
8590
8691void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
87 dest->value = floorl(op1->value / op2->value);
92 dest->value = (__float128)floorl((long double)(op1->value / op2->value));
8893}
8994
9095void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
91 dest->value = fmodl(op1->value, op2->value);
96 dest->value = (__float128)fmodl((long double)op1->value, (long double)op2->value);
9297}
9398
9499void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
95 dest->value = fmodl(fmodl(op1->value, op2->value) + op2->value, op2->value);
100 dest->value = (__float128)fmodl(fmodl((long double)op1->value, (long double)op2->value) + (long double)op2->value, (long double)op2->value);
96101}
97102
98103void bigfloat_write_buf(Buf *buf, const BigFloat *op) {
......@@ -159,5 +164,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {
159164}
160165
161166bool bigfloat_has_fraction(const BigFloat *bigfloat) {
162 return floorl(bigfloat->value) != bigfloat->value;
167 return ((__float128)floorl((long double)bigfloat->value)) != bigfloat->value;
163168}