Skip to content

Commit 07177b4

Browse files
committed
Use machine precision of Pharo
1 parent d7bc47a commit 07177b4

File tree

5 files changed

+31
-103
lines changed

5 files changed

+31
-103
lines changed

src/Math-FunctionFit/PMAnotherGeneticOptimizer.class.st

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Class {
1919

2020
{ #category : #information }
2121
PMAnotherGeneticOptimizer class >> defaultPrecision [
22-
^PMFloatingPointMachine new machinePrecision
22+
23+
^ Float machineEpsilon
2324
]
2425

2526
{ #category : #'instance creation' }

src/Math-FunctionFit/PMGeneralFunctionFit.class.st

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,31 @@ self resetResult.
9292

9393
{ #category : #operation }
9494
PMGeneralFunctionFit >> evaluate [
95-
|ff|
96-
ff :=PMErrorMinimizer function: errorFunction.
97-
firstResult :=[ff evaluate .ff parameters]
98-
onErrorDo: [ verbose ifTrue: [self inform: 'ErrorMinimizer was not successful']. nil].
99-
firstResult ifNotNil: [go addPointAt: firstResult] .
100-
firstResult := go evaluate .
101-
self errorType =#squared
102-
ifTrue: [ff :=PMFunctionFit function: errorFunction function data: errorFunction data ].
103-
ff parameters: firstResult.
104-
ff desiredPrecision: PMFloatingPointMachine new machinePrecision.
105-
ff maximumIterations: 1000 .
106-
result:=[ff evaluate .ff parameters]
107-
onErrorDo: [
108-
verbose ifTrue: [self inform: 'last FunctionFit was not successful'].
109-
ff result parameters].
110-
((errorFunction value: result) > (errorFunction value: firstResult)) ifTrue:[
111-
ff:=result.
112-
result :=firstResult.
113-
firstResult :=ff.
114-
verbose ifTrue: [self inform: 'first result was better than final result' ] ].
115-
^result
95+
96+
| ff |
97+
ff := PMErrorMinimizer function: errorFunction.
98+
firstResult := [
99+
ff evaluate.
100+
ff parameters ] onErrorDo: [
101+
verbose ifTrue: [ self inform: 'ErrorMinimizer was not successful' ].
102+
nil ].
103+
firstResult ifNotNil: [ go addPointAt: firstResult ].
104+
firstResult := go evaluate.
105+
self errorType = #squared ifTrue: [ ff := PMFunctionFit function: errorFunction function data: errorFunction data ].
106+
ff parameters: firstResult.
107+
ff desiredPrecision: Float machineEpsilon.
108+
ff maximumIterations: 1000.
109+
result := [
110+
ff evaluate.
111+
ff parameters ] onErrorDo: [
112+
verbose ifTrue: [ self inform: 'last FunctionFit was not successful' ].
113+
ff result parameters ].
114+
(errorFunction value: result) > (errorFunction value: firstResult) ifTrue: [
115+
ff := result.
116+
result := firstResult.
117+
firstResult := ff.
118+
verbose ifTrue: [ self inform: 'first result was better than final result' ] ].
119+
^ result
116120
]
117121

118122
{ #category : #operation }

src/Math-Helpers/PMFloatingPointMachine.class.st

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Class {
1515
#superclass : #Object,
1616
#instVars : [
1717
'defaultNumericalPrecision',
18-
'machinePrecision',
1918
'negativeMachinePrecision'
2019
],
2120
#classVars : [
@@ -37,19 +36,6 @@ PMFloatingPointMachine class >> reset [
3736
UniqueInstance := nil
3837
]
3938

40-
{ #category : #information }
41-
PMFloatingPointMachine >> computeMachinePrecision [
42-
43-
| one zero inverseRadix tmp |
44-
one := 1.0.
45-
zero := 0.0.
46-
inverseRadix := one / Float radix asFloat.
47-
machinePrecision := one.
48-
[
49-
tmp := one + machinePrecision.
50-
tmp - one = zero ] whileFalse: [ machinePrecision := machinePrecision * inverseRadix ]
51-
]
52-
5339
{ #category : #information }
5440
PMFloatingPointMachine >> computeNegativeMachinePrecision [
5541

@@ -67,17 +53,10 @@ PMFloatingPointMachine >> computeNegativeMachinePrecision [
6753
{ #category : #information }
6854
PMFloatingPointMachine >> defaultNumericalPrecision [
6955

70-
defaultNumericalPrecision ifNil: [ defaultNumericalPrecision := self machinePrecision sqrt ].
56+
defaultNumericalPrecision ifNil: [ defaultNumericalPrecision := Float machineEpsilon sqrt ].
7157
^ defaultNumericalPrecision
7258
]
7359

74-
{ #category : #information }
75-
PMFloatingPointMachine >> machinePrecision [
76-
77-
machinePrecision ifNil: [ self computeMachinePrecision ].
78-
^ machinePrecision
79-
]
80-
8160
{ #category : #information }
8261
PMFloatingPointMachine >> negativeMachinePrecision [
8362

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ PMNumericalMethodsTestCase >> testBissection [
3232
"Code Example 5.1"
3333

3434
| zeroFinder result |
35-
zeroFinder := PMBisectionZeroFinder
36-
function: [ :x | x errorFunction - 0.9 ].
35+
zeroFinder := PMBisectionZeroFinder function: [ :x | x errorFunction - 0.9 ].
3736
zeroFinder
3837
setPositiveX: 10.0;
3938
setNegativeX: 0.0.
4039
result := zeroFinder evaluate.
4140
self assert: zeroFinder hasConverged.
42-
self assert: (result - 1.28155193291605) abs < 1.0e-14
41+
self assert: result closeTo: 1.28155193291605
4342
]
4443

4544
{ #category : #'linear algebra' }
@@ -95,7 +94,7 @@ PMNumericalMethodsTestCase >> testErrorFunction [
9594
"simple cases to expect"
9695

9796
self assert: 0 errorFunction equals: 1 / 2.
98-
self assert: Float fmax errorFunction > (1 - PMFloatingPointMachine new machinePrecision).
97+
self assert: Float fmax errorFunction > (1 - Float machineEpsilon).
9998
"add some code to require initialize to run"
10099
PMErfApproximation reset.
101100
self assert: Float fmax negated errorFunction < Float fmin

0 commit comments

Comments
 (0)