Add config("fraction_space") to control spaces around printing fractions

Added config("tilde_space", boolean) to help/config, along with
a few few minor text improvements.  Updated cal/regress to test
config("tilde_space").

Added config("fraction_space", boolean).  The "fraction_space" controls
whether or not a space (' ') is printed before and after fractions.
By default, config("fraction_space") is false.

For example, with the default, config("fraction_space", 0):

    ; base(1/3),
    ; 1/7
            1/7

With config("fraction_space", 1):

    ; base(1/3),
    ; 1/7
            1 / 7

NOTE: Use of config("fraction_space", 1) can break printing and scanning
      of fractional values via "%r".

NOTE: Use of config("fraction_space", 1) can break printing and scanning
      of complex values via "%c".

Added config("fraction_space", boolean) to help/config, along with
a few few minor text improvements.  Updated cal/regress to test
config("tilde_space").
This commit is contained in:
Landon Curt Noll
2022-12-03 14:39:25 -08:00
parent 7c6723db88
commit 17702a4799
8 changed files with 132 additions and 8 deletions

39
CHANGES
View File

@@ -31,19 +31,52 @@ The following are the changes from calc version 2.14.2.0 to date:
; 1/3 ; 1/3
~0.33333333333333333333 ~0.33333333333333333333
; sqrt(7 + 5i,1e-100)
~2.79305614578749801863+~0.89507688693280053094i
With config("tilde_space", 1): With config("tilde_space", 1):
; 1/3 ; 1/3
~ 0.33333333333333333333 ~ 0.33333333333333333333
; sqrt(7 + 5i,1e-100)
~ 2.79305614578749801863+~ 0.89507688693280053094i
To disable "tilde_space", use config("tilde_space", 0) on the NOTE: Use of config("tilde_space", 1) can break printing and scanning
command line and/or use config("tilde_space", 0),; in your ~/.calcrc. of complex values via "%c".
To enable "tilde_space", use config("tilde_space", 1) on the
command line and/or use config("tilde_space", 1),; in your ~/.calcrc.
Thanks goes to <GitHub use ljramalho> for this suggestion. Thanks goes to <GitHub use ljramalho> for this suggestion.
Added config("tilde_space", boolean) to help/config, along with Added config("tilde_space", boolean) to help/config, along with
a few few minor text improvements. Updated cal/regress to test a few few minor text improvements. Updated cal/regress to test
config("tilde_space") and to account for the new default. config("tilde_space").
Added config("fraction_space", boolean). The "fraction_space" controls
whether or not a space (' ') is printed before and after fractions.
By default, config("fraction_space") is false.
For example, with the default, config("fraction_space", 0):
; base(1/3),
; 1/7
1/7
With config("fraction_space", 1):
; base(1/3),
; 1/7
1 / 7
NOTE: Use of config("fraction_space", 1) can break printing and scanning
of fractional values via "%r".
NOTE: Use of config("fraction_space", 1) can break printing and scanning
of complex values via "%c".
Added config("fraction_space", boolean) to help/config, along with
a few few minor text improvements. Updated cal/regress to test
config("tilde_space").
The following are the changes from calc version 2.14.1.2 to date: The following are the changes from calc version 2.14.1.2 to date:

View File

@@ -541,7 +541,14 @@ define test_config()
vrfy(issimple(config("dupvar_warn")), vrfy(issimple(config("dupvar_warn")),
'562: issimple(config("rdupvar_warn"))'); '562: issimple(config("rdupvar_warn"))');
print '563: Ending test_config';
/* test new space modes */
vrfy(config("tilde_space") == 0,
'563: config("tilde_space") == 0');
vrfy(config("fraction_space") == 0,
'564: config("fraction_space") == 0');
print '565: Ending test_config';
} }
print '010: parsed test_config()'; print '010: parsed test_config()';
@@ -4399,11 +4406,16 @@ define test_strprintf()
vrfy(strprintf("%g", 385) == "3.8e2", vrfy(strprintf("%g", 385) == "3.8e2",
'4851: strprintf("%g", 385) == "3.8e2"'); '4851: strprintf("%g", 385) == "3.8e2"');
c = config("fraction_space", 1);
print '4852: c = config("fraction_space", 1)';
vrfy(strprintf("%r = %f", 27/29, 27/29) == "27 / 29 = .93",
'4853: strprintf("%r = %f", 27/29, 27/29) == "27 / 29 = .93"');
/* restore config */ /* restore config */
c = config("all", callcfg); c = config("all", callcfg);
print '4852: c = config("all", callcfg)'; print '4854: c = config("all", callcfg)';
print '4853: Ending test_strprintf'; print '4855: Ending test_strprintf';
} }
print '088: parsed test_fileop()'; print '088: parsed test_fileop()';

View File

@@ -1223,7 +1223,13 @@ cprintfr(COMPLEX *c)
zprintval(i->num, 0L, 0L); zprintval(i->num, 0L, 0L);
math_chr('i'); math_chr('i');
if (qisfrac(i)) { if (qisfrac(i)) {
if (conf->fraction_space) {
math_chr(' ');
}
math_chr('/'); math_chr('/');
if (conf->fraction_space) {
math_chr(' ');
}
zprintval(i->den, 0L, 0L); zprintval(i->den, 0L, 0L);
} }
} }

View File

@@ -132,6 +132,7 @@ NAMETYPE configs[] = {
{"redecl_warn", CONFIG_REDECL_WARN}, {"redecl_warn", CONFIG_REDECL_WARN},
{"dupvar_warn", CONFIG_DUPVAR_WARN}, {"dupvar_warn", CONFIG_DUPVAR_WARN},
{"hz", CONFIG_HZ}, {"hz", CONFIG_HZ},
{"fraction_space", CONFIG_FRACTION_SPACE},
{NULL, 0} {NULL, 0}
}; };
@@ -199,6 +200,7 @@ CONFIG oldstd = { /* backward compatible standard configuration */
BASEB, /* base for calculations */ BASEB, /* base for calculations */
TRUE, /* warn when redeclaring */ TRUE, /* warn when redeclaring */
TRUE, /* warn when variable names collide */ TRUE, /* warn when variable names collide */
FALSE, /* print spaces around / in fractions */
}; };
CONFIG newstd = { /* new non-backward compatible configuration */ CONFIG newstd = { /* new non-backward compatible configuration */
MODE_INITIAL, /* current output mode */ MODE_INITIAL, /* current output mode */
@@ -260,6 +262,7 @@ CONFIG newstd = { /* new non-backward compatible configuration */
BASEB, /* base for calculations */ BASEB, /* base for calculations */
TRUE, /* warn when redeclaring */ TRUE, /* warn when redeclaring */
TRUE, /* warn when variable names collide */ TRUE, /* warn when variable names collide */
FALSE, /* print spaces around / in fractions */
}; };
CONFIG *conf = NULL; /* loaded in at startup - current configuration */ CONFIG *conf = NULL; /* loaded in at startup - current configuration */
@@ -1006,6 +1009,20 @@ setconfig(int type, VALUE *vp)
not_reached(); not_reached();
break; break;
case CONFIG_FRACTION_SPACE:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->fraction_space = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value for fraction_space");
not_reached();
}
conf->fraction_space = (int)temp;
}
break;
default: default:
math_error("Setting illegal config parameter"); math_error("Setting illegal config parameter");
not_reached(); not_reached();
@@ -1405,6 +1422,10 @@ config_value(CONFIG *cfg, int type, VALUE *vp)
i = CALC_HZ; i = CALC_HZ;
break; break;
case CONFIG_FRACTION_SPACE:
i = (cfg->fraction_space ? 1 : 0);
break;
default: default:
math_error("Getting illegal CONFIG element"); math_error("Getting illegal CONFIG element");
not_reached(); not_reached();

View File

@@ -95,6 +95,7 @@
#define CONFIG_DUPVAR_WARN 45 #define CONFIG_DUPVAR_WARN 45
#define CONFIG_HZ 46 #define CONFIG_HZ 46
#define CONFIG_TILDE_SPACE 47 #define CONFIG_TILDE_SPACE 47
#define CONFIG_FRACTION_SPACE 48
/* /*
@@ -167,6 +168,7 @@ struct config {
int baseb; /* base for calculations */ int baseb; /* base for calculations */
BOOL redecl_warn; /* TRUE => warn of redeclaring variables */ BOOL redecl_warn; /* TRUE => warn of redeclaring variables */
BOOL dupvar_warn; /* TRUE => warn of var name collisions */ BOOL dupvar_warn; /* TRUE => warn of var name collisions */
BOOL fraction_space; /* TRUE => print spaces around / in fractions */
}; };
typedef struct config CONFIG; typedef struct config CONFIG;

1
hash.c
View File

@@ -1002,6 +1002,7 @@ hash_value(int type, void *v, HASH *state)
state = hash_int(type, value->v_config->baseb, state); state = hash_int(type, value->v_config->baseb, state);
state = hash_bool(type, value->v_config->redecl_warn, state); state = hash_bool(type, value->v_config->redecl_warn, state);
state = hash_bool(type, value->v_config->dupvar_warn, state); state = hash_bool(type, value->v_config->dupvar_warn, state);
state = hash_bool(type, value->v_config->fraction_space, state);
break; break;
case V_HASH: case V_HASH:

View File

@@ -382,6 +382,9 @@ DESCRIPTION
printed after leading tilde ('~'). See config("tilde") above. printed after leading tilde ('~'). See config("tilde") above.
If config("tilde") is false, then config("tilde_space") has no effect. If config("tilde") is false, then config("tilde_space") has no effect.
NOTE: Use of config("tilde_space", 1) can break printing and scanning
of complex values via "%c".
The initial "tilde_space" value is 0. The initial "tilde_space" value is 0.
=-= =-=
@@ -899,6 +902,21 @@ DESCRIPTION
This config parameter is read-only and cannot be set. This config parameter is read-only and cannot be set.
=-=
config("fraction_space", boolean)
The "fraction_space" controls whether or not a space (' ') is
printed both before and after '/' when printing a fraction.
NOTE: Use of config("fraction_space", 1) can break printing and scanning
of fractional values via "%r".
NOTE: Use of config("fraction_space", 1) can break printing and scanning
of complex values via "%c".
The initial "fraction_space" value is 0.
EXAMPLE EXAMPLE
; current_cfg = config("all"); ; current_cfg = config("all");
; config("tilde", off),; ; config("tilde", off),;
@@ -961,6 +979,7 @@ EXAMPLE
redecl_warn 1 redecl_warn 1
dupvar_warn 1 dupvar_warn 1
hz 100 hz 100
fraction_space 0
; display() ; display()
20 20

30
qio.c
View File

@@ -420,7 +420,13 @@ qprintfr(NUMBER *q, long width, BOOL force)
{ {
zprintval(q->num, 0L, width); zprintval(q->num, 0L, width);
if (force || qisfrac(q)) { if (force || qisfrac(q)) {
if (conf->fraction_space) {
PUTCHAR(' ');
}
PUTCHAR('/'); PUTCHAR('/');
if (conf->fraction_space) {
PUTCHAR(' ');
}
zprintval(q->den, 0L, width); zprintval(q->den, 0L, width);
} }
} }
@@ -454,7 +460,13 @@ qprintfx(NUMBER *q, long width)
{ {
zprintx(q->num, width); zprintx(q->num, width);
if (qisfrac(q)) { if (qisfrac(q)) {
if (conf->fraction_space) {
PUTCHAR(' ');
}
PUTCHAR('/'); PUTCHAR('/');
if (conf->fraction_space) {
PUTCHAR(' ');
}
zprintx(q->den, 0L); zprintx(q->den, 0L);
} }
} }
@@ -469,7 +481,13 @@ qprintfb(NUMBER *q, long width)
{ {
zprintb(q->num, width); zprintb(q->num, width);
if (qisfrac(q)) { if (qisfrac(q)) {
if (conf->fraction_space) {
PUTCHAR(' ');
}
PUTCHAR('/'); PUTCHAR('/');
if (conf->fraction_space) {
PUTCHAR(' ');
}
zprintb(q->den, 0L); zprintb(q->den, 0L);
} }
} }
@@ -484,7 +502,13 @@ qprintfo(NUMBER *q, long width)
{ {
zprinto(q->num, width); zprinto(q->num, width);
if (qisfrac(q)) { if (qisfrac(q)) {
if (conf->fraction_space) {
PUTCHAR(' ');
}
PUTCHAR('/'); PUTCHAR('/');
if (conf->fraction_space) {
PUTCHAR(' ');
}
zprinto(q->den, 0L); zprinto(q->den, 0L);
} }
} }
@@ -757,6 +781,12 @@ fitprint(NUMBER *q, long width)
width1 = width - width2; width1 = width - width2;
} }
fitzprint(q->num, numdigits, width1); fitzprint(q->num, numdigits, width1);
if (conf->fraction_space) {
PUTCHAR(' ');
}
PUTCHAR('/'); PUTCHAR('/');
if (conf->fraction_space) {
PUTCHAR(' ');
}
fitzprint(q->den, dendigits, width2); fitzprint(q->den, dendigits, width2);
} }