#70216 - 2002-09-24 05:51 PM
Re: Floating Point - Should this simple expression work ?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
quote: But having said that - i don't see the harm in relaxing the rules so that if an expression "CONTAINED" a float - that the float wouldn't be rounded up.
Don't forget it's not just integers and doubles we are talking about - there's unassigned variables and strings too.
Should an expression containing a string variable be considered as a string expression? Should an expression containing an unassigned (null) variable aways evaluate to null?
As far as KiXtart (or any other language) is concerned an expression at it's most complex is always a pair of operands seperated by an operator. The operand may be a variable or constant or another expression.
This means that at the parsing/execution stage a maximum of two operands will be considered - there is no look forward to determine whatever else is in the original expression.
The use of functions returning various data types complicate the issue still further.
It is far better to stick with the existing rules, consistantly adhered to (bugs notwithstanding )
The rule is: In any binary expression the operand on the right is converted to the same type as the operand on the left
Just follow the rules of precedence and the rule always holds true.
If you want to get a real headache, look up the YACC and Bison utilities to get a view of how most modern compilers and interpreters work.
|
|
Top
|
|
|
|
#70221 - 2002-09-24 08:09 PM
Re: Floating Point - Should this simple expression work ?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Told you I'd make an ass out of myself I thought I was proposing until I realized I was summarizing ... my head hurts.
|
|
Top
|
|
|
|
#70224 - 2002-09-25 10:13 AM
Re: Floating Point - Should this simple expression work ?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Hmmm... I didn't word the rule very well.
Of course the operand (variable, constant, function result) is not actually converted - the value of the operand which is on the internal processing stack is converted.
It'd be a nasty surprise if your variables' type was converted because it was on the right hand side of an expression
The rule is more properly: In any binary expression the value of the operand on the right is converted to the same type as the operand on the left
There is probably a better way of putting it though.
|
|
Top
|
|
|
|
#70226 - 2002-09-30 10:13 AM
Re: Floating Point - Should this simple expression work ?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
There is not really any compelling reason to change the way it works at the moment.
The rules are simple and easy to follow, and of course without changing them they are backwards compatible in all cases
Switching the rules to raising the expression to the highest type of the pair is not likely to reduce the number of times a cast (CINT(), CDBL(), CSTR(), VAL()) will be required to ensure that an expression will return the correct type, it just reduces the number of time it may be required for float expressions.
Strings will be particularly vulnerable. It is very common to append a numeric suffix to a string variable, such as
code:
$Filename=$Filename + $Sequence
Which will either cause $Filename to be converted to a numeric type with the type and value of $Sequence.
Of course the rule could apply to numeric types only, and not unassigned, string or other types. The drawback is this creates "special cases" - something to be avoided.
The string confusion could also be resolved by introducing an explicit catenate operator such as "." or ":". (Actually, I think that would be a good move anyway)
Casting both operators (and the result) to the highest type is not a bad thing, but it will break existing scripts, may complicate the rule and doesn't improve the KiXtart language.
|
|
Top
|
|
|
|
#70227 - 2002-09-30 01:11 PM
Re: Floating Point - Should this simple expression work ?
|
Fernando Madruga
Starting to like KiXtart
Registered: 2002-08-21
Posts: 149
Loc: Coimbra.Portugal.Europe.Earth....
|
Special cases are indeed more trouble, but that's more trouble once for the compiler/interpreter creator, as opposed to more trouble each and every time everyone uses some float expression. Just my two cents though... Being a programmer, I tend to spend some time making things easier on the users, if that's something that a lot of people will use some time or some people will use a lot of time... {edit} Also, making KiXtart play according to some more "universal" rules, will only help people used to other languages to make the move to it... It also makes the code a lot easier to follow and to look back at a couple of months later if it's not "plagued" with CInts/CDbls, etc... Surelly, there are some merits to not breaking compatability, but one should not stick to that and not see the world change and react accordingly... {/edit} [ 30. September 2002, 13:18: Message edited by: Fernando Madruga ]
_________________________
Later,
[b]Mad[/b]ruga
|
|
Top
|
|
|
|
#70228 - 2002-09-30 01:54 PM
Re: Floating Point - Should this simple expression work ?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Currently, to ensure that an expression is evaluated as a float and returns a float one option would be:
code:
$floatResult=Cdbl($VariableA) opr $VariableB
You've coerced the first variable to a float so the rest of the expression will be a float.
If you change the rules so that the variables values are coerced to the higher type script writers will have to do things likecode:
$stringResult=$stringVariable + Cstr($Variable)
It's no better or worse really - you've just moved the requirement to cast/coerce the data type to a different situation.
Of course all this applies only where mixed types are in use. Where the variables in an expression have been declared or defined as the same type the problem never arises. If your float variables are initiated as 0.0, and your integers as 0 you won't have a problem in expressions unless you mix them. If you do mix them it is not unreasonable that you should cast them to the correct type for the expression.
The 'C' language does automatically convert operands to the higher of the types, but it it a strongly "typed" language where variable and function declarations have to include a type - and it doesn't have to deal with strings or unknown variable types.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|