Skip to content

Commit dcb9225

Browse files
committed
More approximate numerics
1 parent 5a7a51d commit dcb9225

35 files changed

+6713
-6709
lines changed

mathics/core/util.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def print_expression_tree(expr, indent="", marker=lambda expr: "", file=None, ap
124124
Print a Mathics Expression as an indented tree.
125125
Caller may supply a marker function that computes a marker
126126
to be displayed in the tree for the given node.
127+
The approximate flag causes numbers to be printed with fewer digits
128+
and the number of bits of precision (i.e. Real32 vs Real64) to be
129+
omitted from printing for numpy arrays, to faciliate comparisons
130+
across systems.
127131
"""
128132
if file is None:
129133
file = sys.stdout
@@ -132,17 +136,17 @@ def print_expression_tree(expr, indent="", marker=lambda expr: "", file=None, ap
132136
print(f"{indent}{marker(expr)}{expr}", file=file)
133137
elif not hasattr(expr, "elements"):
134138
if isinstance(expr, MachineReal) and approximate:
135-
#
136-
value = round(expr.value * 1e6) / 1e6
137-
#value = f"{value:.6g}"
139+
# fewer digits
140+
value = str(round(expr.value * 1e6) / 1e6)
138141
elif isinstance(expr, NumericArray) and approximate:
139-
# Real64 and Real32 both become Real*, etc.
142+
# Real32/64->Real*, Integer32/64->Integer*
140143
value = re.sub("[0-9]+,", "*,", str(expr))
141144
else:
142145
value = str(expr)
143146
print(f"{indent}{marker(expr)}{expr.get_head()} {value}", file=file)
144147
if isinstance(expr, NumericArray):
145148
# numpy provides an abbreviated version of the array
149+
# it is inherently approximated (i.e. limited precision) in its own way
146150
na_str = str(expr.value)
147151
i = indent + " "
148152
na_str = i + na_str.replace("\n", "\n" + i)

test/builtin/drawing/test_plot_detail_ref/barchart-cls-def.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ System`Graphics
1212
System`Integer 0
1313
System`List
1414
System`Real 1.06
15-
System`Real 3
15+
System`Real 3.0
1616
System`RGBColor
1717
System`Real 0.447059
1818
System`Real 0.792157
@@ -24,7 +24,7 @@ System`Graphics
2424
System`Integer 0
2525
System`List
2626
System`Real 2.02
27-
System`Real 5
27+
System`Real 5.0
2828
System`RGBColor
2929
System`Real 0.447059
3030
System`Real 0.792157
@@ -36,7 +36,7 @@ System`Graphics
3636
System`Integer 0
3737
System`List
3838
System`Real 2.98
39-
System`Real 2
39+
System`Real 2.0
4040
System`RGBColor
4141
System`Real 0.447059
4242
System`Real 0.792157
@@ -48,7 +48,7 @@ System`Graphics
4848
System`Integer 0
4949
System`List
5050
System`Real 3.94
51-
System`Real 7
51+
System`Real 7.0
5252
System`RGBColor
5353
System`Real 0.447059
5454
System`Real 0.792157
@@ -157,7 +157,7 @@ System`Graphics
157157
System`List
158158
System`Rule
159159
System`ImageMargins
160-
System`Real 0
160+
System`Real 0.0
161161
System`Rule
162162
System`ImagePadding
163163
System`All
@@ -184,7 +184,7 @@ System`Graphics
184184
System`Real 3.94
185185
System`List
186186
System`Integer 0
187-
System`Real 7
187+
System`Real 7.0
188188
System`Rule
189189
System`PlotRangeClipping
190190
System`False

test/builtin/drawing/test_plot_detail_ref/barchart-cls-opt.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ System`Graphics
1212
System`Integer 0
1313
System`List
1414
System`Real 1.06
15-
System`Real 3
15+
System`Real 3.0
1616
System`RGBColor
1717
System`Real 0.447059
1818
System`Real 0.792157
@@ -24,7 +24,7 @@ System`Graphics
2424
System`Integer 0
2525
System`List
2626
System`Real 2.02
27-
System`Real 5
27+
System`Real 5.0
2828
System`RGBColor
2929
System`Real 0.447059
3030
System`Real 0.792157
@@ -36,7 +36,7 @@ System`Graphics
3636
System`Integer 0
3737
System`List
3838
System`Real 2.98
39-
System`Real 2
39+
System`Real 2.0
4040
System`RGBColor
4141
System`Real 0.447059
4242
System`Real 0.792157
@@ -48,7 +48,7 @@ System`Graphics
4848
System`Integer 0
4949
System`List
5050
System`Real 3.94
51-
System`Real 7
51+
System`Real 7.0
5252
System`RGBColor
5353
System`Real 0.447059
5454
System`Real 0.792157
@@ -159,7 +159,7 @@ System`Graphics
159159
System`List
160160
System`Rule
161161
System`ImageMargins
162-
System`Real 0
162+
System`Real 0.0
163163
System`Rule
164164
System`ImagePadding
165165
System`All
@@ -189,7 +189,7 @@ System`Graphics
189189
System`Real 3.94
190190
System`List
191191
System`Integer 0
192-
System`Real 7
192+
System`Real 7.0
193193
System`Rule
194194
System`PlotRangeClipping
195195
System`False

test/builtin/drawing/test_plot_detail_ref/complexplot-vec-def.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ System`Graphics
9191
System`List
9292
System`Rule
9393
System`ImageMargins
94-
System`Real 0
94+
System`Real 0.0
9595
System`Rule
9696
System`ImagePadding
9797
System`All

test/builtin/drawing/test_plot_detail_ref/complexplot-vec-opt.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ System`Graphics
351351
System`List
352352
System`Rule
353353
System`ImageMargins
354-
System`Real 0
354+
System`Real 0.0
355355
System`Rule
356356
System`ImagePadding
357357
System`All

test/builtin/drawing/test_plot_detail_ref/complexplot3d-vec-def.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ System`Graphics3D
124124
System`List
125125
System`Rule
126126
System`ImageMargins
127-
System`Real 0
127+
System`Real 0.0
128128
System`Rule
129129
System`ImagePadding
130130
System`All
@@ -196,7 +196,7 @@ System`Graphics3D
196196
System`List
197197
System`Real 1.3
198198
System`Real -2.4
199-
System`Real 2
199+
System`Real 2.0
200200
System`Rule
201201
System`ViewProjection
202202
System`Automatic

test/builtin/drawing/test_plot_detail_ref/complexplot3d-vec-opt.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ System`Graphics3D
836836
System`List
837837
System`Rule
838838
System`ImageMargins
839-
System`Real 0
839+
System`Real 0.0
840840
System`Rule
841841
System`ImagePadding
842842
System`All
@@ -908,7 +908,7 @@ System`Graphics3D
908908
System`List
909909
System`Real 1.3
910910
System`Real -2.4
911-
System`Real 2
911+
System`Real 2.0
912912
System`Rule
913913
System`ViewProjection
914914
System`Automatic

0 commit comments

Comments
 (0)