diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-12-28 20:55:53 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-12-28 20:55:53 -0800 |
commit | ffada96ffdf3dae7c1932deebd6f512ede5f2aa5 (patch) | |
tree | 08b9fa969e686e01d1456a7e1bdb7407cf9094b5 | |
parent | d6eca506a08d569d4c821c724242fc556638342c (diff) | |
download | txr-ffada96ffdf3dae7c1932deebd6f512ede5f2aa5.tar.gz txr-ffada96ffdf3dae7c1932deebd6f512ede5f2aa5.tar.bz2 txr-ffada96ffdf3dae7c1932deebd6f512ede5f2aa5.zip |
New variables for floating-point printing control.
* lib.c (obj_print_impl): Instead of hard-coded "~s", obtain
the format string for floats from the *print-flo-format*
special variable, whose default value is "~s".
* stream.c (print_flo_digits_s, print_flo_format_s): New symbol
variables.
(formatv): Use *print-flo-digits* value for default precision
for ~f and ~e, rather than hard-coded 3.
(stream_init): Initialize print_flo_digits_s and print_flo_format_s, and
register special variables under those symbols.
* stream.h (print_flo_digits_s, print_flo_format_s): Declared.
* txr.1: Documented new specials.
-rw-r--r-- | lib.c | 5 | ||||
-rw-r--r-- | stream.c | 19 | ||||
-rw-r--r-- | stream.h | 3 | ||||
-rw-r--r-- | txr.1 | 84 |
4 files changed, 92 insertions, 19 deletions
@@ -8406,7 +8406,10 @@ finish: case NUM: case BGNUM: case FLNUM: - format(out, lit("~s"), obj, nao); + { + val fmt = cdr(lookup_var(nil, print_flo_format_s)); + format(out, fmt, obj, nao); + } break; case SYM: if (!pretty) { @@ -64,7 +64,8 @@ val stdin_s, stdout_s, stddebug_s, stderr_s, stdnull_s; -val print_flo_precision_s, print_base_s; +val print_flo_precision_s, print_flo_digits_s, print_flo_format_s; +val print_base_s; val from_start_k, from_current_k, from_end_k; val real_time_k, name_k, fd_k; @@ -2182,7 +2183,8 @@ val formatv(val stream_in, val fmtstr, struct args *al) } state = vf_init, saved_state = vf_init; int width = 0, precision = 0, precision_p = 0, digits = 0, lt = 0, neg = 0; enum align align = al_right; - int sign = 0, zeropad = 0, dfl_precision = 0, print_base = 0; + int sign = 0, zeropad = 0, dfl_precision = 0; + int dfl_digits = 0, print_base = 0; cnum value; cnum arg_ix = 0; @@ -2396,8 +2398,11 @@ val formatv(val stream_in, val fmtstr, struct args *al) chr(ch), obj, nao); } - if (!precision_p) - precision = 3; + if (!precision_p) { + if (!dfl_digits) + dfl_digits = c_num(cdr(lookup_var(nil, print_flo_digits_s))); + precision = dfl_digits; + } /* guard against num_buf overflow */ if (precision > 128) @@ -3311,6 +3316,12 @@ void stream_init(void) reg_var(print_flo_precision_s = intern(lit("*print-flo-precision*"), user_package), num_fast(DBL_DIG)); + reg_var(print_flo_digits_s = intern(lit("*print-flo-digits*"), + user_package), + num_fast(3)); + reg_var(print_flo_format_s = intern(lit("*print-flo-format*"), + user_package), + lit("~s")); reg_var(print_base_s = intern(lit("*print-base*"), user_package), num_fast(10)); @@ -90,6 +90,9 @@ extern val format_s; extern val stdin_s, stdout_s, stddebug_s, stderr_s, stdnull_s; +extern val print_flo_precision_s, print_flo_digits_s, print_flo_format_s; +extern val print_base_s; + void strm_base_init(struct strm_base *s); void strm_base_cleanup(struct strm_base *s); void strm_base_mark(struct strm_base *s); @@ -32016,12 +32016,13 @@ the .code /dev/null device on Unix, but does not involve the operating system. -.coNP Special variable @ *print-flo-precision* +.coNP Special variable @ *print-flo-format* .desc The -.code *print-flo-precision* -special variable holds the precision which applies when -floating-point values are converted to decimal text by the functions +.code *print-flo-format* +variable determines the conversion format which is applied when +a floating-point value is converted to decimal text by the +functions .codn print , .cond pprint , .cond prinl , @@ -32029,15 +32030,48 @@ floating-point values are converted to decimal text by the functions .code tostring and .codn tostringp . -It also applies when the + +The default value is +.codn "~s" . + +The format string must specify the consumption of exactly one argument. + +The conversion string may use embedded width and precision values: +for instance, +.code "~3,4f" +is a valid value for +.codn *print-flo-format* . + +.coNP Special variable @ *print-flo-precision* +.desc +The +.code *print-flo-precision* +special variable specifies the default floating-point printing +precision which is used when the .code ~a -and +or .code ~s -conversion specifiers of the +conversion specifier of the .code format -function are used for printing a floating-point value, and no precision +function is used for printing a floating-point value, and no precision is specified. +Note that since the default value of the variable +.code *print-flo-format* +is the string +.codn "~s" , +the +.code *printf-flo-precision* +variable, by default, also determines the precision which applies when +floating-point values are converted to decimal text by the functions +.codn print , +.cond pprint , +.cond prinl , +.cond pprinl , +.code tostring +and +.codn tostringp . + The default value of .code *print-flo-precision* is that of the @@ -32052,6 +32086,23 @@ to the value of the .code flo-max-dig variable. +.coNP Special variable @ *print-flo-digits* +.desc +The +.code *print-flo-precision* +special variable specifies the default floating-point printing +precision which is used when the +.code ~f +or +.code ~e +conversion specifier of the +.code format +function is used for printing a floating-point value, and no precision +is specified. + +Its default value is +.codn 3 . + .coNP Special variable @ *print-base* .desc The @@ -32393,10 +32444,13 @@ it does not allow an argument of character type). The precision specifier gives the number of digits past the decimal point. The number is rounded off to the specified precision, if necessary. Furthermore, that many digits are always printed, regardless of the actual -precision of the number or its type. If it is omitted, then the default value -is three: three digits past the decimal point. A precision of zero means no -digits pas the decimal point, and in this case the decimal point is suppressed -(regardless of whether the numeric argument is floating-point or integer). +precision of the number or its type. If it is omitted, then the value +is obtained from the special variable +.codn *print-flo-digits* , +whose default value is three: three digits past the decimal point. A precision +of zero means no digits pas the decimal point, and in this case the decimal +point is suppressed (regardless of whether the numeric argument is +floating-point or integer). .coIP e The @@ -32412,8 +32466,10 @@ The precision specifier gives the number of digits past the decimal point printed in the exponential notation, not counting the digits in the exponent. Exactly that many digits are printed, regardless of the precision of the number. If the precision is omitted, then the number of digits after the -decimal point is three. If the precision is zero, then a decimal portion is -truncated off entirely, including the decimal point. +decimal point is obtained from the value of the special variable +.codn *print-flo-digits* , +whose default value is three. If the precision is zero, then a decimal portion +is truncated off entirely, including the decimal point. .coIP p The |