For example
expr (-8.0)**(1.0/3.0)
leads to domain range error, not -2 as expected.
And what about this one:
% tclsh9.0
% set tcl_patchLevel
9.0.2
% expr {(-8)**(1/3)}
1
On 12/1/25 13:31 , erikl wrote:
And what about this one:
% tclsh9.0
% set tcl_patchLevel
9.0.2
% expr {(-8)**(1/3)}
1
And compared these two:
% expr {8**1/3}
2
% expr {8**(1/3)}
1
And compared these two:I don't think your last 2 examples have anything to do with my original question.
% expr {8**1/3}
2
% expr {8**(1/3)}
1
So: parentheses have a "special" effect.
Erik.
Take note of the difference between integer division and floating point division.
For exampleWhen both operands are doubles, the ** operator is implemented as a
expr (-8.0)**(1.0/3.0)
leads to domain range error, not -2 as expected.
Why?
For example
expr (-8.0)**(1.0/3.0)
leads to domain range error, not -2 as expected.
Why?
Regards
Alex
On 12/1/25 07:22, meshparts wrote:
For exampleWhen both operands are doubles, the ** operator is implemented as a
expr (-8.0)**(1.0/3.0)
leads to domain range error, not -2 as expected.
Why?
call to the pow() function from the math library, and it has this
behavior.
Am 01.12.2025 um 14:31 schrieb Don Porter:
On 12/1/25 07:22, meshparts wrote:Still a bug from a mathematical point of view.
For exampleWhen both operands are doubles, the ** operator is implemented as a
expr (-8.0)**(1.0/3.0)
leads to domain range error, not -2 as expected.
Why?
call to the pow() function from the math library, and it has this
behavior.
I mean, even my windows calculator gives me the right result.
The ** operator was introduced in Tcl 8.5, via TIPs 123 & 274.So there is no stadard function in Tcl to compute the power of a
Reproducing pow() was the explicit proposal.
Am 01.12.2025 um 15:04 schrieb Don Porter:
So there is no stadard function in Tcl to compute the power of a
The ** operator was introduced in Tcl 8.5, via TIPs 123 & 274.
Reproducing pow() was the explicit proposal.
negative base to a real exponent?
Take note of the difference between integer division and floating point division.
Am 01.12.2025 um 14:31 schrieb Don Porter:
On 12/1/25 07:22, meshparts wrote:Still a bug from a mathematical point of view.
For exampleWhen both operands are doubles, the ** operator is implemented as a
expr (-8.0)**(1.0/3.0)
leads to domain range error, not -2 as expected.
Why?
call to the pow() function from the math library, and it has this
behavior.
I mean, even my windows calculator gives me the right result.
(1.0000000000000002+1.7320508075688772j)pow(-8.0, 1.0/3.0)
(-8+3.1086244689504383e-15j)pow(pow(-8.0, 1.0/3.0),3)
But on any system and language (Tcl being one among many, if not all) >operating on real numbers, the result can only be NaN.
All that you wrote makes sense, but, interestingly enough, both Perl andNice!
AWK (GAWK) get this right:
In article <10gkad1$1dori$1@dont-email.me>,Also Common Lisp:
Eric Hassold <hassold@evopulse.com> wrote:
...
But on any system and language (Tcl being one among many, if not all) >operating on real numbers, the result can only be NaN.
All that you wrote makes sense, but, interestingly enough, both Perl and
AWK (GAWK) get this right:
% iPerl
:-) -8 ** (1/3)
-2
:-) ^C
% gawk4 'BEGIN { print -8 ** (1/3) }'
-2
%
I haven't dug into it, but (unless I'm assuming something I should not be assuming), I would expect both of them to be using pow() to do the work.
... at system level, since calculating pow(-8.0,1.0/3.0) operates on
floating point numbers, the exponent is not known anymore to be a
rational number when pow() is calculated.
In article <10gkad1$1dori$1@dont-email.me>,
Eric Hassold <hassold@evopulse.com> wrote:
...
But on any system and language (Tcl being one among many, if not all)
operating on real numbers, the result can only be NaN.
All that you wrote makes sense, but, interestingly enough, both Perl and
AWK (GAWK) get this right:
% iPerl
:-) -8 ** (1/3)
-2
:-) ^C
% gawk4 'BEGIN { print -8 ** (1/3) }'
-2
%
I haven't dug into it, but (unless I'm assuming something I should not be assuming), I would expect both of them to be using pow() to do the work.
therefore the only reliable way to do this would be a nthroot functionWell, if this is the only solution, then Tcl needs such a function.
like this:
On 01/12/2025 14:57, Eric Hassold wrote:
... at system level, since calculating pow(-8.0,1.0/3.0) operates on
floating point numbers, the exponent is not known anymore to be a
rational number when pow() is calculated.
Are there irrational floating-point numbers? I thought every
floating-point number (obviously not NaNs and infs) was a rational.
In your example the exponent is a rational number even if it's:
3333333333333333/10000000000000000
as it appears to be on my local Tcl version:
expr {1.0/3.0}
0.3333333333333333
I think this is a misunderstanding. They compute -(8**(1/3)) instead of >(-8)**(1/3)
chris@linux:~> gawk 'BEGIN {print (-8)**(1.0/3)}'
-nan
chris@linux:~>
Though, having said all this, it makes one wonder what OP's actual use case was. I don't think that was ever explained. I mean, what I am saying is that the simplest solution to OP's problem is just to re-parenthesize his expression:As you already imagined, this will not solve my problem.
expect 1.2> expr -8 ** (1.0/3)
Error: domain error: argument not in valid range
expect 1.3> expr -(8 ** (1.0/3))
Result: -2.0
expect 1.4>
Voila! Problem solved.
Am 04.12.2025 um 13:37 schrieb Kenny McCormack:
Though, having said all this, it makes one wonder what OP's actual use case >> was. I don't think that was ever explained. I mean, what I am saying isAs you already imagined, this will not solve my problem.
that the simplest solution to OP's problem is just to re-parenthesize his
expression:
expect 1.2> expr -8 ** (1.0/3)
Error: domain error: argument not in valid range
expect 1.3> expr -(8 ** (1.0/3))
Result: -2.0
expect 1.4>
Voila! Problem solved.
I fixed this in my code by if/else and asking about the sign of the base.
In general Tcl has no standard function to correctly compute $a**(1.0/$b).
I think this needs to be solved.
Note: I don't know enough TCL to know the actual names/syntax for what I am proposing, but it seems you could do something like:
sgn(x)*(abs(x) ** (1.0/n))
In article <10gs0f8$14a4k$1@tota-refugium.de>,
meshparts <alexandru.dadalau@meshparts.de> wrote:
Am 04.12.2025 um 13:37 schrieb Kenny McCormack:
Though, having said all this, it makes one wonder what OP's actual use case >>> was. I don't think that was ever explained. I mean, what I am saying is >>> that the simplest solution to OP's problem is just to re-parenthesize his >>> expression:As you already imagined, this will not solve my problem.
expect 1.2> expr -8 ** (1.0/3)
Error: domain error: argument not in valid range
expect 1.3> expr -(8 ** (1.0/3))
Result: -2.0
expect 1.4>
Voila! Problem solved.
I fixed this in my code by if/else and asking about the sign of the base.
Note: I don't know enough TCL to know the actual names/syntax for what I am proposing, but it seems you could do something like:
sgn(x)*(abs(x) ** (1.0/n))
Editorial comment: IMHO, sgn() is an often useful function that should be present in our toolkits. It is sad that most such toolkits omit it.
In general Tcl has no standard function to correctly compute $a**(1.0/$b). >> I think this needs to be solved.
Agreed.
Am 04.12.2025 um 14:05 schrieb Kenny McCormack:
In article <10gs0f8$14a4k$1@tota-refugium.de>,Christian Gollwitzer already proposed a user defined function to solve
meshparts <alexandru.dadalau@meshparts.de> wrote:
Am 04.12.2025 um 13:37 schrieb Kenny McCormack:
Though, having said all this, it makes one wonder what OP's actualAs you already imagined, this will not solve my problem.
use case
was. I don't think that was ever explained. I mean, what I am
saying is
that the simplest solution to OP's problem is just to re-
parenthesize his
expression:
expect 1.2> expr -8 ** (1.0/3)
Error: domain error: argument not in valid range
expect 1.3> expr -(8 ** (1.0/3))
Result: -2.0
expect 1.4>
Voila! Problem solved.
I fixed this in my code by if/else and asking about the sign of the
base.
Note: I don't know enough TCL to know the actual names/syntax for what
I am
proposing, but it seems you could do something like:
sgn(x)*(abs(x) ** (1.0/n))
Editorial comment: IMHO, sgn() is an often useful function that should be
present in our toolkits. It is sad that most such toolkits omit it.
In general Tcl has no standard function to correctly compute
$a**(1.0/$b).
I think this needs to be solved.
Agreed.
this: nthroot
But this function should be standard in Tcl.
We need a TCL ticket for this.I could write an email to tcl-core, would that suffice?
Any voluntears ?
On 12/3/25 19:10, Tristan Wibberley wrote:
On 01/12/2025 14:57, Eric Hassold wrote:
... at system level, since calculating pow(-8.0,1.0/3.0) operates on
floating point numbers, the exponent is not known anymore to be a
rational number when pow() is calculated.
Are there irrational floating-point numbers? I thought every
floating-point number (obviously not NaNs and infs) was a rational.
In your example the exponent is a rational number even if it's:
3333333333333333/10000000000000000
as it appears to be on my local Tcl version:
expr {1.0/3.0}
0.3333333333333333
Sure
every IEEE754 number is de facto a rational number:
value = (Signbit ? -1 : 1) * (1 + Mantissa) * 2^(Exponent - 1023)--
But that rational number is not (necessarily) equal to the initial
rational number, and the parity of its numerator/denominator is not preserved.
Let's take this 1.0/3.0. The calculated IEEE754 number is:
(0, 01111111101, 0101010101010101010101010101010101010101010101010101) that is:
(1 + (0x5555555555555 / 2**52)) * 2^(1021 - 1023)
or, in pure fractional form:
0x15555555555555 / (2**54)
and, if converted back to decimal, is exactly:
0.333333333333333314829616256247390992939472198486328125
What about 1.0/6.0? well, very similar, we end up with:
0x15555555555555 / (2**55)
i.e., in decimal:
0.1666666666666666574148081281236954964697360992431640625
Then the issue is clear. Those two numbers have same parity (odd
numerator, even denominator), not correlated with the parity of their original fractional form (odd for 1/3, even for 1/6). So if trying to evaluate say pow(-8.0, 1.0/3.0) and pow(-64, 1.0/6.0), it is impossible
to determine, from the IEEE754-encoded "fractional form", that the
former may return -2 but the latter should fail.
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,089 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 153:53:58 |
| Calls: | 13,921 |
| Calls today: | 2 |
| Files: | 187,021 |
| D/L today: |
3,760 files (944M bytes) |
| Messages: | 2,457,163 |