| ... | @@ -5,6 +5,11 @@ | ... | @@ -5,6 +5,11 @@ |
| 5 | * See http://opensource.org/licenses/MIT | 5 | * See http://opensource.org/licenses/MIT |
| 6 | */ | 6 | */ |
| 7 | | 7 | |
| | 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) { |
| 77 | void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { | 82 | void 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 | } |
| 85 | | 90 | |
| 86 | void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { | 91 | void 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 | } |
| 89 | | 94 | |
| 90 | void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { | 95 | void 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 | } |
| 93 | | 98 | |
| 94 | void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) { | 99 | void 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 | } |
| 97 | | 102 | |
| 98 | void bigfloat_write_buf(Buf *buf, const BigFloat *op) { | 103 | void 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 | } |
| 160 | | 165 | |
| 161 | bool bigfloat_has_fraction(const BigFloat *bigfloat) { | 166 | bool 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 | } |