Tcl Source Code

View Ticket
Login
Ticket UUID: 1489011
Title: mathfuncs return strings? numbers?
Type: Bug Created on: 2006-05-15 17:43:52
Submitter: dgp Assigned to: dgp
Subsystem: 47. Bytecode Compiler Severity:
Priority: 5 Medium Last modified: 2006-09-01 03:14:38
Status: Closed Closed by: dgp
Resolution: Duplicate Closed on: 2006-08-31 20:14:38
Version: obsolete: 8.5a5
Description:
dgpIn Tcl 8.4, the only way to create a new math
function is via Tcl_CreateMathFunc

[13:17]dgpUsing that interface, the return type of
the function had to be ont of the supported C types of
the Tcl_Value struct.

[13:18]dgpso mathfuncs were only good for computing
numerical things
[13:18]tclguyyes
[13:18]dgpWith TIP 232 in place, that restriction is
lifted...

[13:20]dgp% proc tcl::mathfunc::append {args} {
return [join $args ""]             
}
% expr {append("foo","bar","soom")}
foobarsoom

[13:21]dgpIs this new ability an intended
improvement?  Or an unintended implementation consquence?
[13:21]dkf_dgp: improvement 
[13:22]dkf_I don't know about "intended" though
[13:22]dgpdon't judge too quickly... 

[13:23]dkf_What I can say is that I know how bad it
was before, having tried to write new functions using
that old API
[13:23]dgp% expr {append("N","a","N")}
domain error: argument not in valid range

[13:24]dgpif the [expr] core is supposed to check for
numeric-ness of the result of a function, then
screening out NaN's might make sense.
[13:24]dgpbut if we intend string functions to work,
we need to stop doing that.
[13:25]dkf_Earlier on, I said that NaN was "the
screaming horror hiding in the space between the 1s and
the 0s" and I meant it

[13:26]spjuthTry: expr {append("0","x","AF")}
[13:26]dgpsame interp:
% expr {sqrt(-1)}
NaN(7ffffffffffff)
[13:26]dgpso either way, the HEAD is currently
buggy/inconsistent
[13:26]dkf_it's the cvtToNumeric opcode that's wacky
[13:26]dgpbut I'd like to know my opinions about
which way correctness lies aren't in gross conflict
with others.
[13:27]dgp% expr {append("0","x","AF")} 
175
[13:30]dkf_What would fail if line tclCompExpr.c:260
was commented out?
[13:30]dgpI'll test and find out...
[13:30]dkf_since I think that's what is causing the
problem
[13:32]dgpwhy is that line inactive in the sqrt()
example?
[13:32]dkf_probably a buggy INST_TRY_CVT_TO_NUMERIC
implementation

[13:34]dgpdkf, mucho failures regarding string rep of
numbers
[13:34]dgp[expr] apparently is supposed to produce
canonical string reps
[13:35]dgp% expr 0x1  
1

spjuthMight be that sqrt(-1) contains a unary minus
operator, thus INST_TRY_ is skipped.
User Comments:
kennykb added on 2006-07-31 05:53:45:
Logged In: YES 
user_id=99768

Ouch. I don't know, either.  When I put the TIP together,
I was thinking only of the new numeric types (chiefly
bigints) and not of strings.

It's obvious that we should try to convert a string to
numeric if we pass it into something (such as an arithmetic
operator) that requires a number.

It's also a historical fact that people use [expr {$x}]
to make $x into a canonical number - it is expected that
[expr 0xff] will return 255.

So it appears, for well or ill, that [expr] is for numbers,
and not for strings, as attractive as the latter seems.
Perhaps we should TRY_CVT_TO_NUMERIC on the result of
math functions?

dgp added on 2006-09-01 03:14:38:
Logged In: YES 
user_id=80530


This is now a Dup of 1541274