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 @@...@@ -5,6 +5,11 @@
5 * See http://opensource.org/licenses/MIT5 * See http://opensource.org/licenses/MIT
6 */6 */
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
8#include "bigfloat.hpp"13#include "bigfloat.hpp"
9#include "bigint.hpp"14#include "bigint.hpp"
10#include "buffer.hpp"15#include "buffer.hpp"
...@@ -46,7 +51,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_...@@ -46,7 +51,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_
46 char *str_begin = (char *)buf_ptr;51 char *str_begin = (char *)buf_ptr;
47 char *str_end;52 char *str_end;
48 errno = 0;53 errno = 0;
49 dest->value = strtold(str_begin, &str_end);54 dest->value = (__float128)strtold(str_begin, &str_end);
50 if (errno) {55 if (errno) {
51 return ErrorOverflow;56 return ErrorOverflow;
52 }57 }
...@@ -77,22 +82,22 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {...@@ -77,22 +82,22 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
77void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {82void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
78 dest->value = op1->value / op2->value;83 dest->value = op1->value / op2->value;
79 if (dest->value >= 0.0) {84 if (dest->value >= 0.0) {
80 dest->value = floorl(dest->value);85 dest->value = (__float128)floorl((long double)dest->value);
81 } else {86 } else {
82 dest->value = ceill(dest->value);87 dest->value = (__float128)ceill((long double)dest->value);
83 }88 }
84}89}
8590
86void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {91void 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));
88}93}
8994
90void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {95void 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);
92}97}
9398
94void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {99void 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);
96}101}
97102
98void bigfloat_write_buf(Buf *buf, const BigFloat *op) {103void bigfloat_write_buf(Buf *buf, const BigFloat *op) {
...@@ -159,5 +164,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {...@@ -159,5 +164,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {
159}164}
160165
161bool bigfloat_has_fraction(const BigFloat *bigfloat) {166bool bigfloat_has_fraction(const BigFloat *bigfloat) {
162 return floorl(bigfloat->value) != bigfloat->value;167 return ((__float128)floorl((long double)bigfloat->value)) != bigfloat->value;
163}168}