Tcl Library Source Code

Check-in [6922235385]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Solve two tickets (one regarding Nelder-Mead and one regarding bigfloat2). Added test cases
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6922235385da0c4b4207dbffdb296a6264f0343a
User & Date: markus 2014-09-21 13:26:54
Context
2014-09-22
23:24
oo::util - Fixed ticket [b3577ed586]. Added missing uplevel 1 which causes evaluation of a delegate in the wrong context. Started a testsuite. Packae version bumped to 1.2.1. check-in: 14faa92003 user: andreask tags: trunk
2014-09-21
13:26
Solve two tickets (one regarding Nelder-Mead and one regarding bigfloat2). Added test cases check-in: 6922235385 user: markus tags: trunk
12:40
Fix problem with detecting exceptions in solving linear programs check-in: 82424135be user: markus tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to modules/math/ChangeLog.

1



2

3
4
5
6
7
8
9
10
11
2014-09-21  Arjen Markus <[email protected]>



	* optimize.tcl: Solve a problem with the detection of the exceptions in solving linear programs. Version 1.0.1

	* optimize.test: Added tests to distnguish infeasible and unbounded linear programs
	* pkgIndex.tcl: Bumping version of math::optimize package to 1.0.1

2014-08-21  Arjen Markus <[email protected]>
	* calculus.tcl: Bumping version to 0.8
	* pkgIndex.tcl: Bumping version of math::calculus package to 0.8

2014-08-21  Arjen Markus <[email protected]>
	* calculus.man: Describe the qk15 procedure implementing Gauss-Kronrod 15 points quadrature rule

>
>
>

>
|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2014-09-21  Arjen Markus <[email protected]>
	* bigfloat2.tcl: Solve ticket UUID 3309165, different implementation of isInt than suggested
	* bigfloat2.test: Added several tests for the new implementation of isInt
	* optimize.tcl: Solve ticket UUID 3193459, as suggested.
	* optimize.tcl: Solve a problem with the detection of the exceptions in solving linear programs. Version 1.0.1
	* optimize.test: Added tests to distinguish infeasible and unbounded linear programs
	* optimize.test: Added test for ticket UUID 3193459
	* pkgIndex.tcl: Bumping version of math::optimize package to 1.0.1, bigfloat2 to 2.0.2

2014-08-21  Arjen Markus <[email protected]>
	* calculus.tcl: Bumping version to 0.8
	* pkgIndex.tcl: Bumping version of math::calculus package to 0.8

2014-08-21  Arjen Markus <[email protected]>
	* calculus.man: Describe the qk15 procedure implementing Gauss-Kronrod 15 points quadrature rule

Changes to modules/math/bigfloat2.tcl.

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50




################################################################################
# procedures that handle floating-point numbers
# these procedures are sorted by name (after eventually removing the underscores)
# 
# BigFloats are internally represented as a list :
# {"F" Mantissa Exponent Delta} where "F" is a character which determins
# the datatype, Mantissa and Delta are two big integers and Exponent another integer.
#
# The BigFloat value equals to (Mantissa +/- Delta)*2^Exponent
# So the internal representation is binary, but trying to get as close as possible to
# the decimal one when converted to a string.
# When calling [fromstr], the Delta parameter is set to the value of 1 at the position
# of the last decimal digit.
# Example : 1.50 belongs to [1.49,1.51], but internally Delta may not equal to 1.
# Because of the binary representation, it is between 1 and 1+(2^-15).
# 
# So Mantissa and Delta are not limited in size, but in practice Delta is kept under
# 2^32 by the 'normalize' procedure, to avoid a never-ended growth of memory used.
# Indeed, when you perform some computations, the Delta parameter (which represent
# the uncertainty on the value of the Mantissa) may increase.
# Exponent, as an integer, is limited to 32 bits, and this limit seems fair.
# The exponent is indeed involved in logarithmic computations, so it may be
# a mistake to give it a too large value.







|











|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50




################################################################################
# procedures that handle floating-point numbers
# these procedures are sorted by name (after eventually removing the underscores)
#
# BigFloats are internally represented as a list :
# {"F" Mantissa Exponent Delta} where "F" is a character which determins
# the datatype, Mantissa and Delta are two big integers and Exponent another integer.
#
# The BigFloat value equals to (Mantissa +/- Delta)*2^Exponent
# So the internal representation is binary, but trying to get as close as possible to
# the decimal one when converted to a string.
# When calling [fromstr], the Delta parameter is set to the value of 1 at the position
# of the last decimal digit.
# Example : 1.50 belongs to [1.49,1.51], but internally Delta may not equal to 1.
# Because of the binary representation, it is between 1 and 1+(2^-15).
#
# So Mantissa and Delta are not limited in size, but in practice Delta is kept under
# 2^32 by the 'normalize' procedure, to avoid a never-ended growth of memory used.
# Indeed, when you perform some computations, the Delta parameter (which represent
# the uncertainty on the value of the Mantissa) may increase.
# Exponent, as an integer, is limited to 32 bits, and this limit seems fair.
# The exponent is indeed involved in logarithmic computations, so it may be
# a mistake to give it a too large value.
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        return [add $piOverTwo [asin [abs $x]]]
    }
    # we always use _asin to compute the result
    # but as it is a Taylor development, the value given to [_asin]
    # has to be a bit smaller than 1 ; by using that trick : acos(x)=asin(sqrt(1-x^2))
    # we can limit the entry of the Taylor development below 1/sqrt(2)
    if {[compare $x [fromstr 0.7071]]>0} {
        # x > sqrt(2)/2 : trying to make _asin converge quickly 
        # creating 0 and 1 with the same precision as the entry
        set fzero [list F 0 -$precision 1]
        # 1.000 with $precision zeros
        set fone [list F [expr {1<<$precision}] -$precision 1]
        # when $x is close to 1 (acos(1.0)=0.0)
        if {[equal $fone $x]} {
            return $fzero







|







98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        return [add $piOverTwo [asin [abs $x]]]
    }
    # we always use _asin to compute the result
    # but as it is a Taylor development, the value given to [_asin]
    # has to be a bit smaller than 1 ; by using that trick : acos(x)=asin(sqrt(1-x^2))
    # we can limit the entry of the Taylor development below 1/sqrt(2)
    if {[compare $x [fromstr 0.7071]]>0} {
        # x > sqrt(2)/2 : trying to make _asin converge quickly
        # creating 0 and 1 with the same precision as the entry
        set fzero [list F 0 -$precision 1]
        # 1.000 with $precision zeros
        set fone [list F [expr {1<<$precision}] -$precision 1]
        # when $x is close to 1 (acos(1.0)=0.0)
        if {[equal $fone $x]} {
            return $fzero
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    if {$expA<$expB} {
        foreach {dummy integerA expA deltaA} $b {break}
        foreach {dummy integerB expB deltaB} $a {break}
    }
    # when we add two numbers which have different digit numbers (after the dot)
    # for example : 1.0 and 0.00001
    # We promote the one with the less number of digits (1.0) to the same level as
    # the other : so 1.00000. 
    # that is why we shift left the number which has the greater exponent
    # But we do not forget the Delta parameter, which is lshift'ed too.
    if {$expA>$expB} {
        set diff [expr {$expA-$expB}]
        set integerA [expr {$integerA<<$diff}]
        set deltaA [expr {$deltaA<<$diff}]
        incr integerA $integerB







|







153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    if {$expA<$expB} {
        foreach {dummy integerA expA deltaA} $b {break}
        foreach {dummy integerB expB deltaB} $a {break}
    }
    # when we add two numbers which have different digit numbers (after the dot)
    # for example : 1.0 and 0.00001
    # We promote the one with the less number of digits (1.0) to the same level as
    # the other : so 1.00000.
    # that is why we shift left the number which has the greater exponent
    # But we do not forget the Delta parameter, which is lshift'ed too.
    if {$expA>$expB} {
        set diff [expr {$expA-$expB}]
        set integerA [expr {$integerA<<$diff}]
        set deltaA [expr {$deltaA<<$diff}]
        incr integerA $integerB
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
################################################################################
proc ::math::bigfloat::_asin {x} {
    # Taylor development
    # asin(x)=x + 1/2 x^3/3 + 3/2.4 x^5/5 + 3.5/2.4.6 x^7/7 + ...
    # into this iterative form :
    # asin(x)=x * (1 + 1/2 * x^2 * (1/3 + 3/4 *x^2 * (...
    # ...* (1/(2n-1) + (2n-1)/2n * x^2 / (2n+1))...)))
    # we show how is really computed the development : 
    # we don't need to set a var with x^n or a product of integers
    # all we need is : x^2, 2n-1, 2n, 2n+1 and a few variables
    foreach {dummy mantissa exp delta} $x {break}
    set precision [expr {-$exp}]
    if {$precision+1<[bits $mantissa]} {
        error "sinus greater than 1"
    }







|







257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
################################################################################
proc ::math::bigfloat::_asin {x} {
    # Taylor development
    # asin(x)=x + 1/2 x^3/3 + 3/2.4 x^5/5 + 3.5/2.4.6 x^7/7 + ...
    # into this iterative form :
    # asin(x)=x * (1 + 1/2 * x^2 * (1/3 + 3/4 *x^2 * (...
    # ...* (1/(2n-1) + (2n-1)/2n * x^2 / (2n+1))...)))
    # we show how is really computed the development :
    # we don't need to set a var with x^n or a product of integers
    # all we need is : x^2, 2n-1, 2n, 2n+1 and a few variables
    foreach {dummy mantissa exp delta} $x {break}
    set precision [expr {-$exp}]
    if {$precision+1<[bits $mantissa]} {
        error "sinus greater than 1"
    }
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
        error "bad result: $l bits"
    }
    while {($int>>($l-1))==0} {
        incr l -1
    }
    return $l
}
    
################################################################################
# returns the integer part of a BigFloat, as a BigInt
# the result is the same one you would have
# if you had called [expr {ceil($x)}]
################################################################################
proc ::math::bigfloat::ceil {number} {
    checkFloat $number







|







500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
        error "bad result: $l bits"
    }
    while {($int>>($l-1))==0} {
        incr l -1
    }
    return $l
}

################################################################################
# returns the integer part of a BigFloat, as a BigInt
# the result is the same one you would have
# if you had called [expr {ceil($x)}]
################################################################################
proc ::math::bigfloat::ceil {number} {
    checkFloat $number
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
################################################################################
# divide A by B and returns the result
# throw error : divide by zero
################################################################################
proc ::math::bigfloat::div {a b} {
    checkNumber $a
    checkNumber $b
    # dispatch to an appropriate procedure 
    if {[isInt $a]} {
        if {[isInt $b]} {
            return [expr {$a/$b}]
        }
        error "trying to divide an integer by a BigFloat"
    }
    if {[isInt $b]} {return [divFloatByInt $a $b]}







|







728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
################################################################################
# divide A by B and returns the result
# throw error : divide by zero
################################################################################
proc ::math::bigfloat::div {a b} {
    checkNumber $a
    checkNumber $b
    # dispatch to an appropriate procedure
    if {[isInt $a]} {
        if {[isInt $b]} {
            return [expr {$a/$b}]
        }
        error "trying to divide an integer by a BigFloat"
    }
    if {[isInt $b]} {return [divFloatByInt $a $b]}
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
        set BMax $temp
    }
    # multiply by zero gives zero
    if {$integerA==0} {
        # why not return any number or the integer 0 ?
        # because there is an exponent that might be different between two BigFloats
        # 0.00 --> exp = -2, 0.000000 -> exp = -6
        return $a 
    }
    # test of the division by zero
    if {$BMin*$BMax<0 || $BMin==0 || $BMax==0} {
        error "divide by zero"
    }
    # shift A because we need accuracy
    set l [bits $integerB]







|







752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
        set BMax $temp
    }
    # multiply by zero gives zero
    if {$integerA==0} {
        # why not return any number or the integer 0 ?
        # because there is an exponent that might be different between two BigFloats
        # 0.00 --> exp = -2, 0.000000 -> exp = -6
        return $a
    }
    # test of the division by zero
    if {$BMin*$BMax<0 || $BMin==0 || $BMax==0} {
        error "divide by zero"
    }
    # shift A because we need accuracy
    set l [bits $integerB]
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
        incr exp -[string length [lindex $tab 1]]
    }
    # this is necessary to ensure we can call fromstr (recursively) with
    # the mantissa ($number)
    if {![string is digit $mantissa]} {
        error "$number is not a number"
    }
    # take account of trailing zeros 
    incr exp -$addzeros
    # multiply $number by 10^$trailingZeros
    append mantissa [string repeat 0 $addzeros]
    # add the sign
    # here we avoid octal numbers by trimming the leading zeros!
    # 2005-10-28 S.ARNOLD
    if {$signe} {set mantissa [expr {-[string trimleft $mantissa 0]}]}







|







1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
        incr exp -[string length [lindex $tab 1]]
    }
    # this is necessary to ensure we can call fromstr (recursively) with
    # the mantissa ($number)
    if {![string is digit $mantissa]} {
        error "$number is not a number"
    }
    # take account of trailing zeros
    incr exp -$addzeros
    # multiply $number by 10^$trailingZeros
    append mantissa [string repeat 0 $addzeros]
    # add the sign
    # here we avoid octal numbers by trimming the leading zeros!
    # 2005-10-28 S.ARNOLD
    if {$signe} {set mantissa [expr {-[string trimleft $mantissa 0]}]}
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
    return 0
}

################################################################################
# checks that n is a BigInt (a number create by math::bignum::fromstr)
################################################################################
proc ::math::bigfloat::isInt {n} {
    if {[llength $n]>1} {
        return 0
    }
    # if {[string is digit $n]} {
        # return 1
    # }
    return 1
}



################################################################################
# returns 1 if x is null, 0 otherwise
################################################################################







|
|
|
<
<
<
|







1164
1165
1166
1167
1168
1169
1170
1171
1172
1173



1174
1175
1176
1177
1178
1179
1180
1181
    return 0
}

################################################################################
# checks that n is a BigInt (a number create by math::bignum::fromstr)
################################################################################
proc ::math::bigfloat::isInt {n} {
    set rc [catch {
        expr {$n%2}
    }]



    return [expr {$rc == 0}]
}



################################################################################
# returns 1 if x is null, 0 otherwise
################################################################################
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
    # we would lose accuracy because small uncertainties add to themselves.
    # Example : 0.0001 + 0.0010 = 0.0011 +/- 0.0002
    # This is quite the same reason that made tcl_precision defaults to 12 :
    # internally, doubles are computed with 17 digits, but to keep precision
    # we need to limit our results to 12.
    # The solution : given a precision target, increment precision with a
    # computed value so that all digits of he result are exacts.
    # 
    # p is the precision
    # pk is the precision increment
    # 2 power pk is also the maximum number of iterations
    # for a number close to 1 but lower than 1,
    # (denom-num)/denum is (in our case) lower than 1/5
    # so the maximum nb of iterations is for:
    # 1/5*(1+1/5*(1/2+1/5*(1/3+1/5*(...))))







|







1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
    # we would lose accuracy because small uncertainties add to themselves.
    # Example : 0.0001 + 0.0010 = 0.0011 +/- 0.0002
    # This is quite the same reason that made tcl_precision defaults to 12 :
    # internally, doubles are computed with 17 digits, but to keep precision
    # we need to limit our results to 12.
    # The solution : given a precision target, increment precision with a
    # computed value so that all digits of he result are exacts.
    #
    # p is the precision
    # pk is the precision increment
    # 2 power pk is also the maximum number of iterations
    # for a number close to 1 but lower than 1,
    # (denom-num)/denum is (in our case) lower than 1/5
    # so the maximum nb of iterations is for:
    # 1/5*(1+1/5*(1/2+1/5*(1/3+1/5*(...))))
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
################################################################################
proc ::math::bigfloat::_sin {x precision delta} {
    # $s holds the result
    set s $x
    # sin(x) = x - x^3/3! + x^5/5! - ... + (-1)^n*x^(2n+1)/(2n+1)!
    #        = x * (1 - x^2/(2*3) * (1 - x^2/(4*5) * (...* (1 - x^2/(2n*(2n+1)) )...)))
    # The second expression allows us to compute the less we can
    
    # $double holds the uncertainty (Delta) of x^2 : 2*(Mantissa*Delta) + Delta^2
    # (Mantissa+Delta)^2=Mantissa^2 + 2*Mantissa*Delta + Delta^2
    set double [expr {$x*$delta>>$precision-1}]
    incr double [expr {1+$delta*$delta>>$precision}]
    # $x holds the Mantissa of x^2
    set x [expr {$x*$x>>$precision}]
    set dt [expr {$x*$delta+$double*($s+$delta)>>$precision}]







|







1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
################################################################################
proc ::math::bigfloat::_sin {x precision delta} {
    # $s holds the result
    set s $x
    # sin(x) = x - x^3/3! + x^5/5! - ... + (-1)^n*x^(2n+1)/(2n+1)!
    #        = x * (1 - x^2/(2*3) * (1 - x^2/(4*5) * (...* (1 - x^2/(2n*(2n+1)) )...)))
    # The second expression allows us to compute the less we can

    # $double holds the uncertainty (Delta) of x^2 : 2*(Mantissa*Delta) + Delta^2
    # (Mantissa+Delta)^2=Mantissa^2 + 2*Mantissa*Delta + Delta^2
    set double [expr {$x*$delta>>$precision-1}]
    incr double [expr {1+$delta*$delta>>$precision}]
    # $x holds the Mantissa of x^2
    set x [expr {$x*$x>>$precision}]
    set dt [expr {$x*$delta+$double*($s+$delta)>>$precision}]
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
#
# Computes the square root of an integer
# Returns an integer
#
proc ::math::bigfloat::_sqrt {n} {
    set i [expr {(([bits $n]-1)/2)+1}]
    set b [expr {$i*2}] ; # Bit to set to get 2^i*2^i
    
    set r 0 ; # guess
    set x 0 ; # guess^2
    set s 0 ; # guess^2 backup
    set t 0 ; # intermediate result
    for {} {$i >= 0} {incr i -1; incr b -2} {
        set x [expr {$s+($t|(1<<$b))}]
        if {abs($x)<= abs($n)} {







|







1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
#
# Computes the square root of an integer
# Returns an integer
#
proc ::math::bigfloat::_sqrt {n} {
    set i [expr {(([bits $n]-1)/2)+1}]
    set b [expr {$i*2}] ; # Bit to set to get 2^i*2^i

    set r 0 ; # guess
    set x 0 ; # guess^2
    set s 0 ; # guess^2 backup
    set t 0 ; # intermediate result
    for {} {$i >= 0} {incr i -1; incr b -2} {
        set x [expr {$s+($t|(1<<$b))}]
        if {abs($x)<= abs($n)} {
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
    # please note: here we call math::bigfloat::tostr
    set result [string trimleft [tostr $x] +]
    set minus ""
    if {[string index $result 0]=="-"} {
        set minus -
        set result [string range $result 1 end]
    }
    
    set l [split $result e]
    set exp 0
    if {[llength $l]==2} {
        # exp : x=Mantissa*2^Exp
        set exp [lindex $l 1]
    }
    # caution with octal numbers : we have to remove heading zeros







|







1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
    # please note: here we call math::bigfloat::tostr
    set result [string trimleft [tostr $x] +]
    set minus ""
    if {[string index $result 0]=="-"} {
        set minus -
        set result [string range $result 1 end]
    }

    set l [split $result e]
    set exp 0
    if {[llength $l]==2} {
        # exp : x=Mantissa*2^Exp
        set exp [lindex $l 1]
    }
    # caution with octal numbers : we have to remove heading zeros
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
    # rounded 'integer' +/- 'delta'
    set up [expr {$result+$delta}]
    set down [expr {$result-$delta}]
    if {($up<0 && $down>0)||($up>0 && $down<0)} {
        # $up>0 and $down<0 or vice-versa : then the number is considered equal to zero
        set isZero yes
	# delta <= 2**n (n = bits(delta))
	# 2**n  <= 10**exp , then 
	# exp >= n.log(2)/log(10)
	# delta <= 10**(n.log(2)/log(10))
        incr exp [expr {int(ceil([bits $delta]*log(2)/log(10)))}]
        set result 0
    } else {
	# iterate until the convergence of the rounding
	# we incr $shift until $up and $down are rounded to the same number







|







2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
    # rounded 'integer' +/- 'delta'
    set up [expr {$result+$delta}]
    set down [expr {$result-$delta}]
    if {($up<0 && $down>0)||($up>0 && $down<0)} {
        # $up>0 and $down<0 or vice-versa : then the number is considered equal to zero
        set isZero yes
	# delta <= 2**n (n = bits(delta))
	# 2**n  <= 10**exp , then
	# exp >= n.log(2)/log(10)
	# delta <= 10**(n.log(2)/log(10))
        incr exp [expr {int(ceil([bits $delta]*log(2)/log(10)))}]
        set result 0
    } else {
	# iterate until the convergence of the rounding
	# we incr $shift until $up and $down are rounded to the same number
2214
2215
2216
2217
2218
2219
2220
2221
        namespace export $function
    }
}

# (AM) No "namespace import" - this should be left to the user!
#namespace import ::math::bigfloat::*

package provide math::bigfloat 2.0.1







|
2211
2212
2213
2214
2215
2216
2217
2218
        namespace export $function
    }
}

# (AM) No "namespace import" - this should be left to the user!
#namespace import ::math::bigfloat::*

package provide math::bigfloat 2.0.2

Changes to modules/math/bigfloat2.test.

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


######################################################
# Begin testsuite
######################################################

proc testSuite {} {
    
    
    # adds 999..9 and 1 -> 1000..0
    for {set i 1} {$i<15} {incr i} {
        assert add 1.0 {tostr [add \
                    [fromstr [string repeat 999 $i]] [fromstr 1]]
        } 1[string repeat 000 $i]
    }
    # sub 1000..0 1 -> 999..9







|
|







61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


######################################################
# Begin testsuite
######################################################

proc testSuite {} {


    # adds 999..9 and 1 -> 1000..0
    for {set i 1} {$i<15} {incr i} {
        assert add 1.0 {tostr [add \
                    [fromstr [string repeat 999 $i]] [fromstr 1]]
        } 1[string repeat 000 $i]
    }
    # sub 1000..0 1 -> 999..9
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                    [string repeat ${j}000 $i]
        }
    }
    # div 10^8 by 1 .. 9
    for {set i 1} {$i<=9} {incr i} {
        assert div 1.3 {tostr [div [fromstr 100000000] [fromstr $i]]} [expr {wide(100000000)/$i}]
    }
    
    
    # 10^8 modulo 1 .. 9
    for {set i 1} {$i<=9} {incr i} {
        assert mod 1.4 {tostr [mod [fromstr 100000000] [fromstr $i]]} [expr {wide(100000000)%$i}]
    }
    
    ################################################################################
    # fromstr problem with octal exponents
    ################################################################################
    fassert fromstr 2.0 {todouble [fromstr 1.0e+099]} 1.0e+099
    fassert fromstr 2.0a {todouble [fromstr 1.0e99]} 1.0e99
    fassert fromstr 2.0b {todouble [fromstr 1.0e-99]} 1.0e-99
    fassert fromstr 2.0c {todouble [fromstr 1.0e-099]} 1.0e-99
    
    
    ################################################################################
    # fromdouble with precision
    ################################################################################
    assert fromdouble 2.1 {tostr [ceil [fromdouble 1.0e99 100]]} 1[zero 99]
    assert fromdouble 2.1a {tostr [fromdouble 1.11 3]} 1.11
    assert fromdouble 2.1b {tostr [fromdouble +1.11 3]} 1.11
    assert fromdouble 2.1c {tostr [fromdouble -1.11 3]} -1.11
    assert fromdouble 2.1d {tostr [fromdouble +01.11 3]} 1.11
    assert fromdouble 2.1e {tostr [fromdouble -01.11 3]} -1.11
    # more to come...
    fassert fromdouble 2.1f {compare [fromdouble [expr {atan(1.0)*4}]] [pi $::tcl_precision]} 0
    
    ################################################################################
    # abs()
    ################################################################################
    proc absTest {version x {int 0}} {
        if {!$int} {
            fassert abs $version {
                tostr [abs [fromstr $x]]
            } [expr {abs($x)}]
        } else {
            assert abs $version {
                tostr [abs [fromstr $x]]
            } [expr {($x<0)?(-$x):$x}]
        }
        
    }
    absTest 2.2a 1.000
    absTest 2.2b -1.000
    absTest 2.2c -0.10
    absTest 2.2d 0 1
    absTest 2.2e 1 1
    absTest 2.2f 10000 1
    absTest 2.2g -1 1
    absTest 2.2h -10000 1
    rename absTest ""
    
    ################################################################################
    # opposite
    ################################################################################
    proc oppTest {version x {int 0}} {
        if {$int} {
            assert opp $version {tostr [opp [fromstr $x]]} [expr {-$x}]
        } else {
            fassert opp $version {tostr [opp [fromstr $x]]} [expr {-$x}]
        }
        
    }
    oppTest 2.3a 1.00
    oppTest 2.3b -1.00
    oppTest 2.3c 0.10
    oppTest 2.3d -0.10
    oppTest 2.3e 0.00
    oppTest 2.3f 1 1
    oppTest 2.3g -1 1
    oppTest 2.3h 0 1
    oppTest 2.3i 100000000 1
    oppTest 2.3j -100000000 1
    rename oppTest ""
    
    ################################################################################
    # equal
    ################################################################################
    proc equalTest {x y} {
        equal [fromstr $x] [fromstr $y]
    }
    assert equal 2.4a {equalTest 0.0 0.1} 1
    assert equal 2.4b {equalTest 0.00 0.10} 0
    assert equal 2.4c {equalTest 0.0 -0.1} 1
    assert equal 2.4d {equalTest 0.00 -0.10} 0
    
    rename equalTest ""
    ################################################################################
    # compare
    ################################################################################
    proc compareTest {x y} {
        compare [fromstr $x] [fromstr $y]
    }
    assert cmp 2.5a {compareTest 0.00 0.10} -1
    assert cmp 2.5b {compareTest 0.1 0.4} -1
    assert cmp 2.5c {compareTest 0.0 -1.0} 1
    assert cmp 2.5d {compareTest -1.0 0.0} -1
    assert cmp 2.5e {compareTest 0.00 0.10} -1
    
    # cleanup
    rename compareTest ""
    
    ################################################################################
    # round
    ################################################################################
    proc roundTest {version x rounded} {
        assert round $version {tostr [round [fromstr $x]]} $rounded
    }
    roundTest 2.6a 0.10 0
    roundTest 2.6b 0.0 0
    roundTest 2.6c 0.50 1
    roundTest 2.6d 0.40 0
    roundTest 2.6e 1.0 1
    roundTest 2.6d -0.40 0
    roundTest 2.6e -0.50 -1
    roundTest 2.6f -1.0 -1
    roundTest 2.6g -1.50 -2
    roundTest 2.6h 1.50 2
    roundTest 2.6i 0.49 0
    roundTest 2.6j -0.49 0
    roundTest 2.6k 1.49 1
    roundTest 2.6l -1.49 -1
    
    
    # cleanup
    rename roundTest ""
    
    ################################################################################
    # floor
    ################################################################################
    proc floorTest {version x} {
        assert floor $version {tostr [floor [fromstr $x]]} [expr {int(floor($x))}]
    }
    floorTest 2.7a 0.10
    floorTest 2.7b 0.90
    floorTest 2.7c 1.0
    floorTest 2.7d -0.10
    floorTest 2.7e -1.0
    
    # cleanup
    rename floorTest ""
    
    ################################################################################
    # ceil
    ################################################################################
    proc ceilTest {version x} {
        assert ceil $version {tostr [ceil [fromstr $x]]} [expr {int(ceil($x))}]
    }
    ceilTest 2.8a 0.10
    ceilTest 2.8b 0.90
    ceilTest 2.8c 1.0
    ceilTest 2.8d -0.10
    ceilTest 2.8e -1.0
    ceilTest 2.8f 0.0
    
    # cleanup
    rename ceilTest ""
    
    ################################################################################
    # BigInt to BigFloat conversion
    ################################################################################
    proc convTest {version x {decimals 1}} {
        assert int2float $version {tostr [int2float [fromstr $x] $decimals]} \
                $x.[string repeat 0 [expr {$decimals-1}]]
    }
    set subversion 0
    foreach decimals {1 2 5 10 100} {
        set version 2.9.$subversion
        fassert int2float $version.0 {tostr [int2float [fromstr 0] $decimals]} 0.0
        convTest $version.1 1 $decimals
        convTest $version.2 5 $decimals
        convTest $version.3 5000000000 $decimals
        incr subversion
    }
    #cleanup
    rename convTest ""
    
    ################################################################################
    # addition
    ################################################################################
    proc addTest {version x y} {
        fassert add $version {todouble [add [fromstr $x] [fromstr $y]]} [expr {$x+$y}]
    }
    addTest 3.0a 1.00 2.00







|
|




|







|
|











|













|










|









|












|










|












|


|




















|
|


|











|


|












|


|


















|







85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                    [string repeat ${j}000 $i]
        }
    }
    # div 10^8 by 1 .. 9
    for {set i 1} {$i<=9} {incr i} {
        assert div 1.3 {tostr [div [fromstr 100000000] [fromstr $i]]} [expr {wide(100000000)/$i}]
    }


    # 10^8 modulo 1 .. 9
    for {set i 1} {$i<=9} {incr i} {
        assert mod 1.4 {tostr [mod [fromstr 100000000] [fromstr $i]]} [expr {wide(100000000)%$i}]
    }

    ################################################################################
    # fromstr problem with octal exponents
    ################################################################################
    fassert fromstr 2.0 {todouble [fromstr 1.0e+099]} 1.0e+099
    fassert fromstr 2.0a {todouble [fromstr 1.0e99]} 1.0e99
    fassert fromstr 2.0b {todouble [fromstr 1.0e-99]} 1.0e-99
    fassert fromstr 2.0c {todouble [fromstr 1.0e-099]} 1.0e-99


    ################################################################################
    # fromdouble with precision
    ################################################################################
    assert fromdouble 2.1 {tostr [ceil [fromdouble 1.0e99 100]]} 1[zero 99]
    assert fromdouble 2.1a {tostr [fromdouble 1.11 3]} 1.11
    assert fromdouble 2.1b {tostr [fromdouble +1.11 3]} 1.11
    assert fromdouble 2.1c {tostr [fromdouble -1.11 3]} -1.11
    assert fromdouble 2.1d {tostr [fromdouble +01.11 3]} 1.11
    assert fromdouble 2.1e {tostr [fromdouble -01.11 3]} -1.11
    # more to come...
    fassert fromdouble 2.1f {compare [fromdouble [expr {atan(1.0)*4}]] [pi $::tcl_precision]} 0

    ################################################################################
    # abs()
    ################################################################################
    proc absTest {version x {int 0}} {
        if {!$int} {
            fassert abs $version {
                tostr [abs [fromstr $x]]
            } [expr {abs($x)}]
        } else {
            assert abs $version {
                tostr [abs [fromstr $x]]
            } [expr {($x<0)?(-$x):$x}]
        }

    }
    absTest 2.2a 1.000
    absTest 2.2b -1.000
    absTest 2.2c -0.10
    absTest 2.2d 0 1
    absTest 2.2e 1 1
    absTest 2.2f 10000 1
    absTest 2.2g -1 1
    absTest 2.2h -10000 1
    rename absTest ""

    ################################################################################
    # opposite
    ################################################################################
    proc oppTest {version x {int 0}} {
        if {$int} {
            assert opp $version {tostr [opp [fromstr $x]]} [expr {-$x}]
        } else {
            fassert opp $version {tostr [opp [fromstr $x]]} [expr {-$x}]
        }

    }
    oppTest 2.3a 1.00
    oppTest 2.3b -1.00
    oppTest 2.3c 0.10
    oppTest 2.3d -0.10
    oppTest 2.3e 0.00
    oppTest 2.3f 1 1
    oppTest 2.3g -1 1
    oppTest 2.3h 0 1
    oppTest 2.3i 100000000 1
    oppTest 2.3j -100000000 1
    rename oppTest ""

    ################################################################################
    # equal
    ################################################################################
    proc equalTest {x y} {
        equal [fromstr $x] [fromstr $y]
    }
    assert equal 2.4a {equalTest 0.0 0.1} 1
    assert equal 2.4b {equalTest 0.00 0.10} 0
    assert equal 2.4c {equalTest 0.0 -0.1} 1
    assert equal 2.4d {equalTest 0.00 -0.10} 0

    rename equalTest ""
    ################################################################################
    # compare
    ################################################################################
    proc compareTest {x y} {
        compare [fromstr $x] [fromstr $y]
    }
    assert cmp 2.5a {compareTest 0.00 0.10} -1
    assert cmp 2.5b {compareTest 0.1 0.4} -1
    assert cmp 2.5c {compareTest 0.0 -1.0} 1
    assert cmp 2.5d {compareTest -1.0 0.0} -1
    assert cmp 2.5e {compareTest 0.00 0.10} -1

    # cleanup
    rename compareTest ""

    ################################################################################
    # round
    ################################################################################
    proc roundTest {version x rounded} {
        assert round $version {tostr [round [fromstr $x]]} $rounded
    }
    roundTest 2.6a 0.10 0
    roundTest 2.6b 0.0 0
    roundTest 2.6c 0.50 1
    roundTest 2.6d 0.40 0
    roundTest 2.6e 1.0 1
    roundTest 2.6d -0.40 0
    roundTest 2.6e -0.50 -1
    roundTest 2.6f -1.0 -1
    roundTest 2.6g -1.50 -2
    roundTest 2.6h 1.50 2
    roundTest 2.6i 0.49 0
    roundTest 2.6j -0.49 0
    roundTest 2.6k 1.49 1
    roundTest 2.6l -1.49 -1


    # cleanup
    rename roundTest ""

    ################################################################################
    # floor
    ################################################################################
    proc floorTest {version x} {
        assert floor $version {tostr [floor [fromstr $x]]} [expr {int(floor($x))}]
    }
    floorTest 2.7a 0.10
    floorTest 2.7b 0.90
    floorTest 2.7c 1.0
    floorTest 2.7d -0.10
    floorTest 2.7e -1.0

    # cleanup
    rename floorTest ""

    ################################################################################
    # ceil
    ################################################################################
    proc ceilTest {version x} {
        assert ceil $version {tostr [ceil [fromstr $x]]} [expr {int(ceil($x))}]
    }
    ceilTest 2.8a 0.10
    ceilTest 2.8b 0.90
    ceilTest 2.8c 1.0
    ceilTest 2.8d -0.10
    ceilTest 2.8e -1.0
    ceilTest 2.8f 0.0

    # cleanup
    rename ceilTest ""

    ################################################################################
    # BigInt to BigFloat conversion
    ################################################################################
    proc convTest {version x {decimals 1}} {
        assert int2float $version {tostr [int2float [fromstr $x] $decimals]} \
                $x.[string repeat 0 [expr {$decimals-1}]]
    }
    set subversion 0
    foreach decimals {1 2 5 10 100} {
        set version 2.9.$subversion
        fassert int2float $version.0 {tostr [int2float [fromstr 0] $decimals]} 0.0
        convTest $version.1 1 $decimals
        convTest $version.2 5 $decimals
        convTest $version.3 5000000000 $decimals
        incr subversion
    }
    #cleanup
    rename convTest ""

    ################################################################################
    # addition
    ################################################################################
    proc addTest {version x y} {
        fassert add $version {todouble [add [fromstr $x] [fromstr $y]]} [expr {$x+$y}]
    }
    addTest 3.0a 1.00 2.00
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    addTest 3.0j 0 -1.00
    addTest 3.0k 2.00 1
    addTest 3.0l -2.00 1
    addTest 3.0m 1.00 0
    addTest 3.0n -1.00 0
    #cleanup
    rename addTest ""
    
    ################################################################################
    # substraction
    ################################################################################
    proc subTest {version x y} {
        fassert sub $version {todouble [sub [fromstr $x] [fromstr $y]]} [expr {$x-$y}]
    }
    subTest 3.1a 1.00 2.00







|







285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    addTest 3.0j 0 -1.00
    addTest 3.0k 2.00 1
    addTest 3.0l -2.00 1
    addTest 3.0m 1.00 0
    addTest 3.0n -1.00 0
    #cleanup
    rename addTest ""

    ################################################################################
    # substraction
    ################################################################################
    proc subTest {version x y} {
        fassert sub $version {todouble [sub [fromstr $x] [fromstr $y]]} [expr {$x-$y}]
    }
    subTest 3.1a 1.00 2.00
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    subTest 3.1l 2.00 1
    subTest 3.1m 1.00 2
    subTest 3.1n -1.00 1
    subTest 3.1o 0.00 2
    subTest 3.1p 2.00 0
    # cleanup
    rename subTest ""
    
    ################################################################################
    # multiplication
    ################################################################################
    proc mulTest {version x y} {
        fassert mul $version {todouble [mul [fromstr $x] [fromstr $y]]} [expr {$x*$y}]
    }
    proc mulInt {version x y} {







|







310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    subTest 3.1l 2.00 1
    subTest 3.1m 1.00 2
    subTest 3.1n -1.00 1
    subTest 3.1o 0.00 2
    subTest 3.1p 2.00 0
    # cleanup
    rename subTest ""

    ################################################################################
    # multiplication
    ################################################################################
    proc mulTest {version x y} {
        fassert mul $version {todouble [mul [fromstr $x] [fromstr $y]]} [expr {$x*$y}]
    }
    proc mulInt {version x y} {
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    mulInt 3.2h 1 2.00
    mulInt 3.2i 1 -2.00
    mulInt 3.2j 0 2.00
    mulInt 3.2k 0 -2.00
    mulInt 3.2l 10 2.00
    mulInt 3.2m 10 -2.00
    mulInt 3.2n 1 0.00
    
    
    # cleanup
    rename mulTest ""
    rename mulInt ""
    
    ################################################################################
    # division
    ################################################################################
    proc divTest {version x y} {
        fassert div $version {
            string trimright [todouble [div [fromstr $x] [fromstr $y]]] 0
        } [string trimright [expr {$x/$y}] 0]
    }
    
    
    divTest 3.3a 1.00 2.00
    divTest 3.3b 2.00 1.00
    divTest 3.3c -1.00 2.00
    divTest 3.3d 1.00 -2.00
    divTest 3.3e 2.00 -1.00
    divTest 3.3f -2.00 1.00
    divTest 3.3g -1.00 -2.00
    divTest 3.3h -2.00 -1.00
    divTest 3.3i 0.0 1.0
    divTest 3.3j 0.0 -1.0
    
    # cleanup
    rename divTest ""
    
    ################################################################################
    # rest of the division
    ################################################################################
    proc modTest {version x y} {
        fassert mod $version {
            todouble [mod [fromstr $x] [fromstr $y]]
        } [expr {fmod($x,$y)}]
    }
    
    modTest 3.4a 1.00 2.00
    modTest 3.4b 2.00 1.00
    modTest 3.4c -1.00 2.00
    modTest 3.4d 1.00 -2.00
    modTest 3.4e 2.00 -1.00
    modTest 3.4f -2.00 1.00
    modTest 3.4g -1.00 -2.00
    modTest 3.4h -2.00 -1.00
    modTest 3.4i 0.0 1.0
    modTest 3.4j 0.0 -1.0
    
    modTest 3.4k 1.00 2
    modTest 3.4l 2.00 1
    modTest 3.4m -1.00 2
    modTest 3.4n -2.00 1
    modTest 3.4o 0.0 1
    modTest 3.4p 1.50 1
    
    # cleanup
    rename modTest ""
    
    ################################################################################
    # divide a BigFloat by an integer
    ################################################################################
    proc divTest {version x y} {
        fassert div $version {todouble [div [fromstr $x] [fromstr $y]]} \
            [expr {double(round(1000*$x/$y))/1000.0}]
    }
    set subversion 0
    foreach a {1.0000 -1.0000} {
        foreach b {2 3} {
            divTest 3.5.$subversion $a $b
            incr subversion
        }
    }
    
    # cleanup
    rename divTest ""
    
    ################################################################################
    # pow : takes a float to an integer power (>0)
    ################################################################################
    proc powTest {version x y {int 0}} {
        if {!$int} {
            fassert pow $version {todouble [pow [fromstr $x 14] [fromstr $y]]}\
                    [expr [join [string repeat "[string trimright $x 0] " $y] *]]







|
|



|








|
|










|


|








|










|






|


|














|


|







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    mulInt 3.2h 1 2.00
    mulInt 3.2i 1 -2.00
    mulInt 3.2j 0 2.00
    mulInt 3.2k 0 -2.00
    mulInt 3.2l 10 2.00
    mulInt 3.2m 10 -2.00
    mulInt 3.2n 1 0.00


    # cleanup
    rename mulTest ""
    rename mulInt ""

    ################################################################################
    # division
    ################################################################################
    proc divTest {version x y} {
        fassert div $version {
            string trimright [todouble [div [fromstr $x] [fromstr $y]]] 0
        } [string trimright [expr {$x/$y}] 0]
    }


    divTest 3.3a 1.00 2.00
    divTest 3.3b 2.00 1.00
    divTest 3.3c -1.00 2.00
    divTest 3.3d 1.00 -2.00
    divTest 3.3e 2.00 -1.00
    divTest 3.3f -2.00 1.00
    divTest 3.3g -1.00 -2.00
    divTest 3.3h -2.00 -1.00
    divTest 3.3i 0.0 1.0
    divTest 3.3j 0.0 -1.0

    # cleanup
    rename divTest ""

    ################################################################################
    # rest of the division
    ################################################################################
    proc modTest {version x y} {
        fassert mod $version {
            todouble [mod [fromstr $x] [fromstr $y]]
        } [expr {fmod($x,$y)}]
    }

    modTest 3.4a 1.00 2.00
    modTest 3.4b 2.00 1.00
    modTest 3.4c -1.00 2.00
    modTest 3.4d 1.00 -2.00
    modTest 3.4e 2.00 -1.00
    modTest 3.4f -2.00 1.00
    modTest 3.4g -1.00 -2.00
    modTest 3.4h -2.00 -1.00
    modTest 3.4i 0.0 1.0
    modTest 3.4j 0.0 -1.0

    modTest 3.4k 1.00 2
    modTest 3.4l 2.00 1
    modTest 3.4m -1.00 2
    modTest 3.4n -2.00 1
    modTest 3.4o 0.0 1
    modTest 3.4p 1.50 1

    # cleanup
    rename modTest ""

    ################################################################################
    # divide a BigFloat by an integer
    ################################################################################
    proc divTest {version x y} {
        fassert div $version {todouble [div [fromstr $x] [fromstr $y]]} \
            [expr {double(round(1000*$x/$y))/1000.0}]
    }
    set subversion 0
    foreach a {1.0000 -1.0000} {
        foreach b {2 3} {
            divTest 3.5.$subversion $a $b
            incr subversion
        }
    }

    # cleanup
    rename divTest ""

    ################################################################################
    # pow : takes a float to an integer power (>0)
    ################################################################################
    proc powTest {version x y {int 0}} {
        if {!$int} {
            fassert pow $version {todouble [pow [fromstr $x 14] [fromstr $y]]}\
                    [expr [join [string repeat "[string trimright $x 0] " $y] *]]
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618







619
620
621
622
623
624
625
    set subversion 0
    foreach a {1 2 3} {
        foreach b {2 3 5 8} {
            powTest 3.7.$subversion $a $b 1
            incr subversion
        }
    }
    
    # cleanup
    rename powTest ""
    
    
    ################################################################################
    # pi constant and angles conversion
    ################################################################################
    fassert pi 3.8.0 {todouble [pi 16]} [expr {atan(1)*4}]
    # converts Pi -> 180�
    fassert rad2deg 3.8.1 {todouble [rad2deg [pi 20]]} 180.0
    # converts 180� -> Pi
    fassert deg2rad 3.8.2 {todouble [deg2rad [fromstr 180.0 20]]} [expr {atan(1.0)*4}]
    
    
    ################################################################################
    # iszero : the precision is too small to determinate the number
    ################################################################################
    
    assert iszero 4.0a {iszero [fromstr 0]} 1
    assert iszero 4.0b {iszero [fromstr 0.0]} 1
    assert iszero 4.0c {iszero [fromstr 1]} 0
    assert iszero 4.0d {iszero [fromstr 1.0]} 0
    assert iszero 4.0e {iszero [fromstr -1]} 0
    assert iszero 4.0f {iszero [fromstr -1.0]} 0
    
    ################################################################################
    # sqrt : square root
    ################################################################################
    proc sqrtTest {version x} {
        fassert sqrt $version {todouble [sqrt [fromstr $x 18]]} [expr {sqrt($x)}]
    }
    sqrtTest 4.1a 1.
    sqrtTest 4.1b 0.001
    sqrtTest 4.1c 0.004
    sqrtTest 4.1d 4.
    
    # cleanup
    rename sqrtTest ""
    
    
    ################################################################################
    # expTest : exponential function
    ################################################################################
    proc expTest {version x} {
        fassert exp $version {todouble [exp [fromstr $x 17]]} [expr {exp($x)}]
    }
    
    expTest 4.2a 1.
    expTest 4.2b 0.001
    expTest 4.2c 0.004
    expTest 4.2d 40.
    expTest 4.2e -0.001
    
    # cleanup
    rename expTest ""
    
    ################################################################################
    # logTest : logarithm
    ################################################################################
    proc logTest {version x} {
        fassert log $version {todouble [log [fromstr $x 17]]} [expr {log($x)}]
    }
    
    logTest 4.3a 1.0
    logTest 4.3b 0.001
    logTest 4.3c 0.004
    logTest 4.3d 40.
    logTest 4.3e 1[zero 10].0
    
    # cleanup
    rename logTest ""
    
    ################################################################################
    # cos & sin : trigonometry
    ################################################################################
    proc cosEtSin {version quartersOfPi} {
        set x [div [mul [pi 18] [fromstr $quartersOfPi]] [fromstr 4]]
        #fassert cos {todouble [cos $x]} [expr {cos(atan(1)*$quartersOfPi)}]
        #fassert sin {todouble [sin $x]} [expr {sin(atan(1)*$quartersOfPi)}]
        fassert cos $version.0 {todouble [cos $x]} [expr {cos([todouble $x])}]
        fassert sin $version.1 {todouble [sin $x]} [expr {sin([todouble $x])}]
    }
    
    fassert cos 4.4.0.0 {todouble [cos [fromstr 0. 17]]} [expr {cos(0)}]
    fassert sin 4.4.0.1 {todouble [sin [fromstr 0. 17]]} [expr {sin(0)}]
    foreach i {1 2 3 4 5 6 7 8} {
        cosEtSin 4.4.$i $i
    }
    
    
    # cleanup
    rename cosEtSin ""
    
    ################################################################################
    # tan & cotan : trigonometry
    ################################################################################
    proc tanCotan {version i} {
        upvar pi pi
        set x [div [mul $pi [fromstr $i]] [fromstr 10]]
        set double [expr {atan(1)*(double($i)*0.4)}]
        fassert cos $version.0 {todouble [cos $x]} [expr {cos($double)}]
        fassert sin $version.1 {todouble [sin $x]} [expr {sin($double)}]
        fassert tan $version.2 {todouble [tan $x]} [expr {tan($double)}]
        fassert cotan $version.3 {todouble [cotan $x]} [expr {double(1.0)/tan($double)}]
    }
    
    set pi [pi 20]
    set subversion 0
    foreach i {1 2 3 6 7 8 9} {
        tanCotan 4.5.$subversion $i
        incr subversion
    }
    
    
    # cleanup
    rename tanCotan ""
    
    
    ################################################################################
    # atan , asin & acos : trigonometry (inverse functions)
    ################################################################################
    proc atanTest {version x} {
        set f [fromstr $x 20]
        fassert atan $version.0 {todouble [atan $f]} [expr {atan($x)}]
        if {abs($x)<=1.0} {
            fassert acos $version.1 {todouble [acos $f]} [expr {acos($x)}]
            fassert asin $version.2 {todouble [asin $f]} [expr {asin($x)}]
        }
    }
    set subversion 0
    atanTest 4.6.0.0 0.0
    foreach i {1 2 3 4 5 6 7 8 9} {
        atanTest 4.6.1.$subversion 0.$i
        atanTest 4.6.2.$subversion $i.0
        atanTest 4.6.3.$subversion -0.$i
        atanTest 4.6.4.$subversion -$i.0
        incr subversion
    }
    
    # cleanup
    rename atanTest ""
    
    ################################################################################
    # cosh , sinh & tanh : hyperbolic functions
    ################################################################################
    proc hyper {version x} {
        set f [fromstr $x 18]
        fassert cosh $version.0 {todouble [cosh $f]} [expr {cosh($x)}]
        fassert sinh $version.1 {todouble [sinh $f]} [expr {sinh($x)}]
        fassert tanh $version.2 {todouble [tanh $f]} [expr {tanh($x)}]
    }
    
    hyper 4.7.0 0.0
    set subversion 0
    foreach i {1 2 3 4 5 6 7 8 9} {
        hyper 4.7.1.$subversion 0.$i
        hyper 4.7.2.$subversion $i.0
        hyper 4.7.3.$subversion -0.$i
        hyper 4.7.4.$subversion -$i.0
    }
    
    # cleanup
    rename hyper ""
    
	################################################################################
	# tostr with -nosci option
	################################################################################
	set version 5.0
	fassert tostr-nosci $version.0 {tostr -nosci [fromstr 23450.e+7]} 234500000000.
	fassert tostr-nosci $version.1 {tostr -nosci [fromstr 23450.e-7]} 0.002345
	fassert tostr-nosci $version.2 {tostr -nosci [fromstr 23450000]} 23450000.
	fassert tostr-nosci $version.3 {tostr -nosci [fromstr 2345.0]} 2345.







}

testSuite
################################################################################
# end of testsuite for bigfloat 1.0
################################################################################
# cleanup global procs







|


|
|








|
|



|






|










|


|
|






|





|


|






|





|


|










|





|
|


|












|






|
|


|
|




















|


|









|








|


|








>
>
>
>
>
>
>







439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
    set subversion 0
    foreach a {1 2 3} {
        foreach b {2 3 5 8} {
            powTest 3.7.$subversion $a $b 1
            incr subversion
        }
    }

    # cleanup
    rename powTest ""


    ################################################################################
    # pi constant and angles conversion
    ################################################################################
    fassert pi 3.8.0 {todouble [pi 16]} [expr {atan(1)*4}]
    # converts Pi -> 180�
    fassert rad2deg 3.8.1 {todouble [rad2deg [pi 20]]} 180.0
    # converts 180� -> Pi
    fassert deg2rad 3.8.2 {todouble [deg2rad [fromstr 180.0 20]]} [expr {atan(1.0)*4}]


    ################################################################################
    # iszero : the precision is too small to determinate the number
    ################################################################################

    assert iszero 4.0a {iszero [fromstr 0]} 1
    assert iszero 4.0b {iszero [fromstr 0.0]} 1
    assert iszero 4.0c {iszero [fromstr 1]} 0
    assert iszero 4.0d {iszero [fromstr 1.0]} 0
    assert iszero 4.0e {iszero [fromstr -1]} 0
    assert iszero 4.0f {iszero [fromstr -1.0]} 0

    ################################################################################
    # sqrt : square root
    ################################################################################
    proc sqrtTest {version x} {
        fassert sqrt $version {todouble [sqrt [fromstr $x 18]]} [expr {sqrt($x)}]
    }
    sqrtTest 4.1a 1.
    sqrtTest 4.1b 0.001
    sqrtTest 4.1c 0.004
    sqrtTest 4.1d 4.

    # cleanup
    rename sqrtTest ""


    ################################################################################
    # expTest : exponential function
    ################################################################################
    proc expTest {version x} {
        fassert exp $version {todouble [exp [fromstr $x 17]]} [expr {exp($x)}]
    }

    expTest 4.2a 1.
    expTest 4.2b 0.001
    expTest 4.2c 0.004
    expTest 4.2d 40.
    expTest 4.2e -0.001

    # cleanup
    rename expTest ""

    ################################################################################
    # logTest : logarithm
    ################################################################################
    proc logTest {version x} {
        fassert log $version {todouble [log [fromstr $x 17]]} [expr {log($x)}]
    }

    logTest 4.3a 1.0
    logTest 4.3b 0.001
    logTest 4.3c 0.004
    logTest 4.3d 40.
    logTest 4.3e 1[zero 10].0

    # cleanup
    rename logTest ""

    ################################################################################
    # cos & sin : trigonometry
    ################################################################################
    proc cosEtSin {version quartersOfPi} {
        set x [div [mul [pi 18] [fromstr $quartersOfPi]] [fromstr 4]]
        #fassert cos {todouble [cos $x]} [expr {cos(atan(1)*$quartersOfPi)}]
        #fassert sin {todouble [sin $x]} [expr {sin(atan(1)*$quartersOfPi)}]
        fassert cos $version.0 {todouble [cos $x]} [expr {cos([todouble $x])}]
        fassert sin $version.1 {todouble [sin $x]} [expr {sin([todouble $x])}]
    }

    fassert cos 4.4.0.0 {todouble [cos [fromstr 0. 17]]} [expr {cos(0)}]
    fassert sin 4.4.0.1 {todouble [sin [fromstr 0. 17]]} [expr {sin(0)}]
    foreach i {1 2 3 4 5 6 7 8} {
        cosEtSin 4.4.$i $i
    }


    # cleanup
    rename cosEtSin ""

    ################################################################################
    # tan & cotan : trigonometry
    ################################################################################
    proc tanCotan {version i} {
        upvar pi pi
        set x [div [mul $pi [fromstr $i]] [fromstr 10]]
        set double [expr {atan(1)*(double($i)*0.4)}]
        fassert cos $version.0 {todouble [cos $x]} [expr {cos($double)}]
        fassert sin $version.1 {todouble [sin $x]} [expr {sin($double)}]
        fassert tan $version.2 {todouble [tan $x]} [expr {tan($double)}]
        fassert cotan $version.3 {todouble [cotan $x]} [expr {double(1.0)/tan($double)}]
    }

    set pi [pi 20]
    set subversion 0
    foreach i {1 2 3 6 7 8 9} {
        tanCotan 4.5.$subversion $i
        incr subversion
    }


    # cleanup
    rename tanCotan ""


    ################################################################################
    # atan , asin & acos : trigonometry (inverse functions)
    ################################################################################
    proc atanTest {version x} {
        set f [fromstr $x 20]
        fassert atan $version.0 {todouble [atan $f]} [expr {atan($x)}]
        if {abs($x)<=1.0} {
            fassert acos $version.1 {todouble [acos $f]} [expr {acos($x)}]
            fassert asin $version.2 {todouble [asin $f]} [expr {asin($x)}]
        }
    }
    set subversion 0
    atanTest 4.6.0.0 0.0
    foreach i {1 2 3 4 5 6 7 8 9} {
        atanTest 4.6.1.$subversion 0.$i
        atanTest 4.6.2.$subversion $i.0
        atanTest 4.6.3.$subversion -0.$i
        atanTest 4.6.4.$subversion -$i.0
        incr subversion
    }

    # cleanup
    rename atanTest ""

    ################################################################################
    # cosh , sinh & tanh : hyperbolic functions
    ################################################################################
    proc hyper {version x} {
        set f [fromstr $x 18]
        fassert cosh $version.0 {todouble [cosh $f]} [expr {cosh($x)}]
        fassert sinh $version.1 {todouble [sinh $f]} [expr {sinh($x)}]
        fassert tanh $version.2 {todouble [tanh $f]} [expr {tanh($x)}]
    }

    hyper 4.7.0 0.0
    set subversion 0
    foreach i {1 2 3 4 5 6 7 8 9} {
        hyper 4.7.1.$subversion 0.$i
        hyper 4.7.2.$subversion $i.0
        hyper 4.7.3.$subversion -0.$i
        hyper 4.7.4.$subversion -$i.0
    }

    # cleanup
    rename hyper ""

	################################################################################
	# tostr with -nosci option
	################################################################################
	set version 5.0
	fassert tostr-nosci $version.0 {tostr -nosci [fromstr 23450.e+7]} 234500000000.
	fassert tostr-nosci $version.1 {tostr -nosci [fromstr 23450.e-7]} 0.002345
	fassert tostr-nosci $version.2 {tostr -nosci [fromstr 23450000]} 23450000.
	fassert tostr-nosci $version.3 {tostr -nosci [fromstr 2345.0]} 2345.

	################################################################################
	# tests for isInt - ticket 3309165
	################################################################################
	assert isInt $version.0 {isInt 12345678901234} 1
	assert isInt $version.1 {isInt 12345678901234.0} 0
	assert isInt $version.1 {isInt not-a-number} 0
}

testSuite
################################################################################
# end of testsuite for bigfloat 1.0
################################################################################
# cleanup global procs

Changes to modules/math/optimize.tcl.

731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	    }
	    incr i
	}

	# Return if the relative error is within an acceptable range

	set rerror [expr { 2. * abs( $yTop - $yBot )
			   / ( abs( $yTop ) + abs( $yBot ) ) }]
	if { $rerror < $params(-ftol) } {
	    set status ok
	    break
	}

	# Count iterations








|







731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	    }
	    incr i
	}

	# Return if the relative error is within an acceptable range

	set rerror [expr { 2. * abs( $yTop - $yBot )
			   / ( abs( $yTop ) + abs( $yBot ) + $params(-ftol) ) }]
	if { $rerror < $params(-ftol) } {
	    set status ok
	    break
	}

	# Count iterations

Changes to modules/math/optimize.test.

596
597
598
599
600
601
602



















603
604
605
606
607
608
609
	foreach {x y} $dd(x) break
	expr { abs($x-0.774561) < 0.00005 && abs($y-0.755644) < 0.00005 }
    } \
    -cleanup {
	rename g {}; unset dd
    } \
    -result 1




















testsuiteCleanup

#  Restore precision
set ::tcl_precision $old_precision

# Local Variables:







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
	foreach {x y} $dd(x) break
	expr { abs($x-0.774561) < 0.00005 && abs($y-0.755644) < 0.00005 }
    } \
    -cleanup {
	rename g {}; unset dd
    } \
    -result 1

# Make sure the method deals gracefully with a "valley"
# (Ticket UUID: 3193459)

test nelderMead-2.5 "Nelder-Mead - indeterminate minimum (valley)" \
    -setup {
	proc h {a b} {
	    return [expr {abs($a-$b)}]
	}
    } \
    -body {
	array set dd [::math::optimize::nelderMead h {1. 1.}]
	foreach {x y} $dd(x) break
	expr { abs($x-1.) < 0.00005 && abs($y-1.) < 0.00005 }
    } \
    -cleanup {
	rename h {}; unset dd
    } \
    -result 1

testsuiteCleanup

#  Restore precision
set ::tcl_precision $old_precision

# Local Variables:

Changes to modules/math/pkgIndex.tcl.

21
22
23
24
25
26
27
28
29
30
31
32
package ifneeded math::linearalgebra     1.1.4 [list source [file join $dir linalg.tcl]]
package ifneeded math::bignum            3.1.1 [list source [file join $dir bignum.tcl]]
package ifneeded math::bigfloat          1.2.2 [list source [file join $dir bigfloat.tcl]]
package ifneeded math::machineparameters 0.1   [list source [file join $dir machineparameters.tcl]]

if {![package vsatisfies [package provide Tcl] 8.5]} {return}
package ifneeded math::calculus::symdiff 1.0   [list source [file join $dir symdiff.tcl]]
package ifneeded math::bigfloat          2.0.1 [list source [file join $dir bigfloat2.tcl]]
package ifneeded math::numtheory         1.0   [list source [file join $dir numtheory.tcl]]
package ifneeded math::decimal           1.0.3 [list source [file join $dir decimal.tcl]]









|




21
22
23
24
25
26
27
28
29
30
31
32
package ifneeded math::linearalgebra     1.1.4 [list source [file join $dir linalg.tcl]]
package ifneeded math::bignum            3.1.1 [list source [file join $dir bignum.tcl]]
package ifneeded math::bigfloat          1.2.2 [list source [file join $dir bigfloat.tcl]]
package ifneeded math::machineparameters 0.1   [list source [file join $dir machineparameters.tcl]]

if {![package vsatisfies [package provide Tcl] 8.5]} {return}
package ifneeded math::calculus::symdiff 1.0   [list source [file join $dir symdiff.tcl]]
package ifneeded math::bigfloat          2.0.2 [list source [file join $dir bigfloat2.tcl]]
package ifneeded math::numtheory         1.0   [list source [file join $dir numtheory.tcl]]
package ifneeded math::decimal           1.0.3 [list source [file join $dir decimal.tcl]]