From b7835be8db1d6d2ef60bbe9e972cb708c0fee8c0 Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Sun, 18 Jan 2026 13:28:25 +0100 Subject: =?UTF-8?q?Allow=20p=3D=E2=88=9E=20in=20program=20and=20adjust=20a?= =?UTF-8?q?nd=20generalize=20printing=20accordingly.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- double.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'double.c') diff --git a/double.c b/double.c index 09fcc28..0799e7d 100644 --- a/double.c +++ b/double.c @@ -8,6 +8,10 @@ double int_to_valuetype(int i){ return (double) i; } double int_to_exptype(double d){ return d; } +double infinity_to_exptype(){ return INFINITY; } + +bool exptype_is_infinite(double d){ return d==INFINITY; } + double sum(double d1, double d2){ return d1+d2; } double difference(double d1, double d2){ return d1-d2; } @@ -31,26 +35,38 @@ double power(double d, double p) { return pow(d,p); } double valuetype_to_double(double d){ return d; } -double exptype_to_double(double d){ return d; } - int valuetype_to_string(char* s, double d){ - sprintf(s,"%f",d); + if( d == 0.0 || d == 1.0 || d == 2.0 || d == 4.0 || d == 8.0 || d == 16.0 ) sprintf(s,"%d", (int)d); + else sprintf(s,"%4.3f",d); return 0; } -int valuetype_to_latex(char* s, double d){ - if( d == 0.0) sprintf(s,"$0$"); - else if( d == 1.0) sprintf(s,"$1$"); - else sprintf(s,"$%4.3f$",d); - return 0; +int valuetype_to_latex(char* s, double d){ return valuetype_to_string(s,d); } + +int exptype_to_string(char *s, double p){ + if(exptype_is_infinite(p)){ + sprintf(s,"inf"); + return 0; + } + else return valuetype_to_string(s,p); +} + +int exptype_to_latex(char *s, double p){ + if(exptype_is_infinite(p)){ + sprintf(s,"\\infty"); + return 0; + } + else return valuetype_to_latex(s,p); } int root_to_string(char* s, double d, double p){ - valuetype_to_string(s,pow(d,1.0/p)); + if(exptype_is_infinite(p)) valuetype_to_string(s,d); + else valuetype_to_string(s,pow(d,1.0/p)); return 0; } int root_to_latex(char* s, double d, double p){ - valuetype_to_latex(s,pow(d,1.0/p)); + if(exptype_is_infinite(p)) valuetype_to_latex(s,d); + else valuetype_to_latex(s,pow(d,1.0/p)); return 0; } -- cgit v1.2.3