diff --git a/silly.c b/silly.c index a163753..5e47bf2 100644 --- a/silly.c +++ b/silly.c @@ -96,3 +96,23 @@ silly make_silly(short sign, int before, int after) { s.after = after; return s; } + +silly silly_from_float(float x) { + silly s; + double _; + s.sign = signbit(x); + x = fabs(x); + s.before = trunc(x); + s.after = modf(x, &_); + return s; +} + +silly silly_from_double(double x) { + silly s; + double _; + s.sign = signbit(x); + x = fabs(x); + s.before = trunc(x); + s.after = modf(x, &_); + return s; +} diff --git a/silly.h b/silly.h index 1d75e95..ec16936 100644 --- a/silly.h +++ b/silly.h @@ -1,3 +1,4 @@ +#include #include #include #include @@ -15,8 +16,8 @@ silly silly_add(silly, silly); silly silly_sub(silly, silly); silly silly_mul(silly, silly); -//silly silly_from_32(float); -//silly silly_from_64(double); +silly silly_from_float(float); +silly silly_from_double(double); double silly_to_double(silly); char* silly_to_string(silly); diff --git a/tests/test.c b/tests/test.c index e7cac3e..9f8f9b3 100644 --- a/tests/test.c +++ b/tests/test.c @@ -100,6 +100,17 @@ TEST silly_conversion() { x.before = 1; x.after = (int)(((double)0xffffffff)/10); ASSERT(-1.1 - silly_to_double(x) <= 0.01); + + x = silly_from_float(0.0); + ASSERT_EQ_FMT(0.0, silly_to_double(x), "%f"); + x = silly_from_float(-1.1); + ASSERT(-1.1 - silly_to_double(x) <= 0.01); + + x = silly_from_double((double) 0.0); + ASSERT_EQ_FMT(0.0, silly_to_double(x), "%f"); + x = silly_from_double((double) -1.1); + ASSERT(-1.1 - silly_to_double(x) <= 0.01); + PASS(); }