Skip to content

Commit 9639fc1

Browse files
committed
Use Pharo radix as it is the same as PolyMath one
1 parent 770f386 commit 9639fc1

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

src/Math-Helpers/PMFloatingPointMachine.class.st

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Class {
2020
#superclass : #Object,
2121
#instVars : [
2222
'defaultNumericalPrecision',
23-
'radix',
2423
'machinePrecision',
2524
'negativeMachinePrecision',
2625
'smallestNumber',
@@ -42,6 +41,8 @@ PMFloatingPointMachine class >> new [
4241

4342
{ #category : #accessing }
4443
PMFloatingPointMachine class >> reset [
44+
45+
<script>
4546
UniqueInstance := nil
4647
]
4748

@@ -50,14 +51,17 @@ PMFloatingPointMachine >> computeLargestNumber [
5051

5152
| one floatingRadix fullMantissaNumber |
5253
one := 1.0.
53-
floatingRadix := self radix asFloat.
54-
fullMantissaNumber := one - ( floatingRadix * self negativeMachinePrecision).
54+
floatingRadix := Float radix asFloat.
55+
fullMantissaNumber := one - (floatingRadix * self negativeMachinePrecision).
5556
largestNumber := fullMantissaNumber.
56-
[ [ fullMantissaNumber := fullMantissaNumber * floatingRadix.
57-
fullMantissaNumber isInfinite ifTrue: [^nil].
58-
largestNumber := fullMantissaNumber.
59-
true] whileTrue: [ ].
60-
] on: Error do: [ :signal | signal return: nil]
57+
[
58+
[
59+
fullMantissaNumber := fullMantissaNumber * floatingRadix.
60+
fullMantissaNumber isInfinite ifTrue: [ ^ nil ].
61+
largestNumber := fullMantissaNumber.
62+
true ] whileTrue: [ ] ]
63+
on: Error
64+
do: [ :signal | signal return: nil ]
6165
]
6266

6367
{ #category : #information }
@@ -66,11 +70,11 @@ PMFloatingPointMachine >> computeMachinePrecision [
6670
| one zero inverseRadix tmp |
6771
one := 1.0.
6872
zero := 0.0.
69-
inverseRadix := one / self radix asFloat.
73+
inverseRadix := one / Float radix asFloat.
7074
machinePrecision := one.
71-
[ tmp := one + machinePrecision.
72-
tmp - one = zero]
73-
whileFalse:[ machinePrecision := machinePrecision * inverseRadix]
75+
[
76+
tmp := one + machinePrecision.
77+
tmp - one = zero ] whileFalse: [ machinePrecision := machinePrecision * inverseRadix ]
7478
]
7579

7680
{ #category : #information }
@@ -79,47 +83,31 @@ PMFloatingPointMachine >> computeNegativeMachinePrecision [
7983
| one zero floatingRadix inverseRadix tmp |
8084
one := 1.0.
8185
zero := 0.0.
82-
floatingRadix := self radix asFloat.
86+
floatingRadix := Float radix asFloat.
8387
inverseRadix := one / floatingRadix.
8488
negativeMachinePrecision := one.
85-
[ tmp := one - negativeMachinePrecision.
86-
tmp - one = zero]
87-
whileFalse:[ negativeMachinePrecision := negativeMachinePrecision * inverseRadix]
88-
]
89-
90-
{ #category : #information }
91-
PMFloatingPointMachine >> computeRadix [
92-
93-
| one zero a b tmp1 tmp2|
94-
one := 1.0.
95-
zero := 0.0.
96-
a := one.
97-
[ a := a + a.
98-
tmp1 := a + one.
99-
tmp2 := tmp1 - a.
100-
tmp2 - one = zero] whileTrue:[].
101-
b := one.
102-
[ b := b + b.
103-
tmp1 := a + b.
104-
radix := ( tmp1 - a) truncated.
105-
radix = 0 ] whileTrue: []
89+
[
90+
tmp := one - negativeMachinePrecision.
91+
tmp - one = zero ] whileFalse: [ negativeMachinePrecision := negativeMachinePrecision * inverseRadix ]
10692
]
10793

10894
{ #category : #information }
10995
PMFloatingPointMachine >> computeSmallestNumber [
11096

11197
| one floatingRadix inverseRadix fullMantissaNumber |
11298
one := 1 asFloat.
113-
floatingRadix := self radix asFloat.
99+
floatingRadix := Float radix asFloat.
114100
inverseRadix := one / floatingRadix.
115101
fullMantissaNumber := one - (floatingRadix * self negativeMachinePrecision).
116102
smallestNumber := fullMantissaNumber.
117-
[[fullMantissaNumber := fullMantissaNumber * inverseRadix.
118-
fullMantissaNumber = 0.0 ifTrue: [Error signal ].
103+
[
104+
[
105+
fullMantissaNumber := fullMantissaNumber * inverseRadix.
106+
fullMantissaNumber = 0.0 ifTrue: [ Error signal ].
119107
smallestNumber := fullMantissaNumber.
120-
true]
121-
whileTrue: []]
122-
on: Error do: [:signal | signal return: nil]
108+
true ] whileTrue: [ ] ]
109+
on: Error
110+
do: [ :signal | signal return: nil ]
123111
]
124112

125113
{ #category : #information }
@@ -157,21 +145,12 @@ PMFloatingPointMachine >> negativeMachinePrecision [
157145
^ negativeMachinePrecision
158146
]
159147

160-
{ #category : #information }
161-
PMFloatingPointMachine >> radix [
162-
163-
radix ifNil: [ self computeRadix ].
164-
^ radix
165-
]
166-
167148
{ #category : #display }
168149
PMFloatingPointMachine >> showParameters [
169150

170151
Transcript cr; cr;
171152
nextPutAll: 'Floating-point machine parameters'; cr;
172-
nextPutAll: '---------------------------------';cr;
173-
nextPutAll: 'Radix: '.
174-
self radix printOn: Transcript.
153+
nextPutAll: '---------------------------------'.
175154
Transcript cr; nextPutAll: 'Machine precision: '.
176155
self machinePrecision printOn: Transcript.
177156
Transcript cr; nextPutAll: 'Negative machine precision: '.

src/Math-Tests-Numerical/PMFloatingPointMachineTestCase.class.st

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ Class {
66

77
{ #category : #precision }
88
PMFloatingPointMachineTestCase >> testMachinePrecision [
9+
910
| mach |
1011
mach := PMFloatingPointMachine new.
1112
self assert: mach machinePrecision > 0.
1213
self assert: mach machinePrecision < 1.
1314
self assert: mach negativeMachinePrecision > 0.
1415
self assert: mach negativeMachinePrecision < 1.
15-
self
16-
assert: (mach radix raisedTo: mach negativeMachinePrecision)
17-
equals: 1.0.
18-
self assert: (mach radix raisedTo: mach machinePrecision) equals: 1.0
16+
self assert: (Float radix raisedTo: mach negativeMachinePrecision) equals: 1.0.
17+
self assert: (Float radix raisedTo: mach machinePrecision) equals: 1.0
1918
]
2019

2120
{ #category : #precision }
2221
PMFloatingPointMachineTestCase >> testMachinePrecisionIsFloatPrecision [
2322
"sanity check. Take logs to find the exponent, then compare to Float's method"
2423

2524
| prec |
26-
prec := (PMFloatingPointMachine new machinePrecision ln
27-
/ PMFloatingPointMachine new radix ln) negated.
25+
prec := (PMFloatingPointMachine new machinePrecision ln / Float radix ln) negated.
2826
self assert: prec equals: Float precision
2927
]
3028

0 commit comments

Comments
 (0)