Page 1 of 1 1
Topic Options
#78973 - 2002-06-14 10:29 AM BUG: Double variable inside string not evaluated.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
KiXtart environment:
Windows 95
KiXtart 4.10 RC2.

While playing with the new variable types I noticed that "double" variables within strings are not evaluated. Integers are fine. Here is some code which illustrates this:
code:
$v=0.0+3/2
"v=" $v ", type=" VarTypeName($v) ?
"v=$v, type=" VarTypeName($v) ?
$v=3/2
"v=" $v ", type=" VarTypeName($v) ?
"v=$v, type=" VarTypeName($v) ?

Results are:
quote:
v=1, type=Double
v=, type=Double
v=1, type=Long
v=1, type=Long


Top
#78974 - 2002-06-14 10:39 AM Re: BUG: Double variable inside string not evaluated.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
this might be actually good way to go.
as it is preferred syntax not to put variables inside strings.
if this is really ruud's intend, info needed in documentation.
if not, bug. right?

also ruud,
is it suppose to be that 2/3 is 1?
_________________________
!

download KiXnet

Top
#78975 - 2002-06-14 04:22 PM Re: BUG: Double variable inside string not evaluated.
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I think we figured that one out.
2/3 = 1
but
2.0/3.0=0.666666666666
which is in accordance to the documentation for Kixtart 4.10RC2
_________________________
There are two types of vessels, submarines and targets.

Top
#78976 - 2002-06-14 10:03 PM Re: BUG: Double variable inside string not evaluated.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
jens, surely we did figure lot's of things, but I would like to hear what ruud has to say once he reads the post.
I know how it works now, but I want to know was this ruud's intension to make it happen like this.

because it still is not as other type conversions.
$x= "aa"+3

is that going to be a string or integer?

{edit}
the accordance, this I don't quite follow...
automatic type conversion I found on doc but where in doc is that 3/2 = 1?

[ 14 June 2002, 22:05: Message edited by: Lonkero ]
_________________________
!

download KiXnet

Top
#78977 - 2002-06-17 09:46 AM Re: BUG: Double variable inside string not evaluated.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Lonkero,
The rsult of "aa" + 3 is a string "aa3", which is exactly the same as all other conversions, i.e. the result of every expression is a value of the type of the coercing element.

Here is a good example. Have a guess what the result of this will be:
code:
$x="x" + 3.0/2 + 5.0/2 + 7/2

It's not as obvious as you might at first think. The answer is:
code:
$x=x1.52.53, type=String

The reason for this is precedence. You have to take each sub expression abd resolve them first, in the order of precedence:
quote:
3.0/2 = 1.5 (coercing value is a float)
5.0/2 = 2.3 (coercing value is a float)
7/2 = 3 (coercing value is an integer)
"x"+1.5 = "x1.5" (coercing value is a string)
"x1.5"+2.5 = "x1.52.5" (coercing value is a string)
"x1.52.5"+3 = "x1.52.53" (coercing value is a string)

The result is correct and is consistant with all the type conversion rules.

The problem is that it is horribly confusing and will make coding with the new types a complete nightmare. You will have to be so careful when constructing expressions.

Because of automatic type conversion it is all too easy to convert your double to an integer, then use that in another expression later on.
The variable may have the right value, but it will have the wrong type, so expressions which use it may give the wrong results..

This will make debugging very hard, and means that the new variable types are not reliable.

Unless it is simple the only way to ensure that your code is absolutely safe is to wrap each element of the expression in the new type conversion functions, i.e.
code:
$y=1
$x=5+$y/3

won't work because both "5" and $y are integers.
code:
$y=1
$x=CDbl(5)+Cdbl($y)/Cdbl(3)

Will always work, whether $y is an integer or double as Cdbl() coerces the type.

Top
#78978 - 2002-06-17 02:03 PM Re: BUG: Double variable inside string not evaluated.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
mm...
I think I'm getting there. finally.
so,
0.0+5/2 will produce floating type but with value 2 because division is done BEFORE conversion.
_________________________
!

download KiXnet

Top
#78979 - 2002-06-18 09:18 AM Re: BUG: Double variable inside string not evaluated.
Ruud van Velsen Moderator Offline
Developer
*****

Registered: 1999-05-06
Posts: 391
Loc: Amsterdam, The Netherlands
In short: absolutely correct [Smile]

The type of a (sub)expression is based on the leftmost operand. And in the last example, 5/2 is evaluated first (due to operator precedence).

Ruud

Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 657 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.062 seconds in which 0.031 seconds were spent on a total of 12 queries. Zlib compression enabled.