Verified Commit b5b67c93 authored by Camil Staps's avatar Camil Staps 🚀

Clean up & centralize format strings used for reals

parent 52e44f31
Pipeline #29157 passed with stages
in 12 minutes and 20 seconds
......@@ -352,7 +352,7 @@ unsigned int print_instruction(int to_stderr, struct program *pgm, uint32_t i) {
WPRINTF(w, " %d", abs((int)pgm->code[i] / IF_INT_64_OR_32(8,4)));
break;
case 'r': /* Real constant */
WPRINTF(w, " %.15g", (*(BC_REAL*)&pgm->code[i]) + 0.0);
WPRINTF(w, BC_REAL_FMT, (*(BC_REAL*)&pgm->code[i]) + 0.0);
break;
case 'a': /* Arity */
WPRINTF(w, " %d", abs((int16_t) ((BC_WORD_S)pgm->code[i] >> IF_INT_64_OR_32(48,16))));
......
......@@ -18,6 +18,7 @@
# define BC_WORD_FMT_HEX "%"SCNx64
# define BC_WORD_S_FMT "%"SCNd64
# define BC_REAL double
# define BC_REAL_SCAN_FMT "%lg"
#else
# define BC_WORD uint32_t
# define BC_WORD_S int32_t
......@@ -25,7 +26,9 @@
# define BC_WORD_FMT_HEX "%x"
# define BC_WORD_S_FMT "%d"
# define BC_REAL float
# define BC_REAL_SCAN_FMT "%g"
#endif
#define BC_REAL_FMT "%.15g"
#define BC_BOOL uint8_t
......
......@@ -68,7 +68,7 @@ instr_RtoAC :: !Target -> Target
instr_RtoAC t = foldl (flip append) t
[ "{"
, "char r[22];"
, "int n=sprintf(r,\"%.15g\",*((BC_REAL*)bsp)+0.0);"
, "int n=sprintf(r,BC_REAL_FMT,*((BC_REAL*)bsp)+0.0);"
, "NEED_HEAP(2+((n+IF_INT_64_OR_32(7,3))>>IF_INT_64_OR_32(3,2)));"
, "hp[0]=(BC_WORD)&__STRING__+2;"
, "hp[1]=n;"
......@@ -254,11 +254,11 @@ instr_readFR t = foldl (flip append) t
, "struct file *f=(struct file*)bsp[1];"
, "bsp-=2;"
, "if (f==&clean_stdinout) {"
, "\tbsp[0]=fscanf(stdin,\"%lg\",(BC_REAL*)&bsp[1])==1;"
, "\tbsp[0]=fscanf(stdin,BC_REAL_SCAN_FMT,(BC_REAL*)&bsp[1])==1;"
, "} else if (f==&clean_stderr) {"
, "\tIO_error(\"FReadI: can't read from StdErr\");"
, "} else if (F_IS_TEXT_MODE(f->file_mode)) {"
, "\tbsp[0]=fscanf(f->file_handle,\"%lg\",(BC_REAL*)&bsp[1])==1;"
, "\tbsp[0]=fscanf(f->file_handle,BC_REAL_SCAN_FMT,(BC_REAL*)&bsp[1])==1;"
, "} else {"
, "\tint i;"
, "\tbsp[0]=1;"
......@@ -422,11 +422,11 @@ instr_writeFR t = foldl (flip append) t
, "bsp++;"
, "struct file *f=(struct file*)bsp[1];"
, "if (f==&clean_stdinout)"
, "\tPRINTF(\"%.15g\",r);"
, "\tPRINTF(BC_REAL_FMT,r);"
, "else if (f==&clean_stderr)"
, "\tEPRINTF(\"%.15g\",r);"
, "\tEPRINTF(BC_REAL_FMT,r);"
, "else if (F_IS_TEXT_MODE(f->file_mode))"
, "\tfprintf(f->file_handle,\"%.15g\",r);"
, "\tfprintf(f->file_handle,BC_REAL_FMT,r);"
, "else {"
, "\tBC_WORD i=bsp[-1];"
, "\tputc(((char*)&i)[0],f->file_handle);"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment