Menu

#14 Trap door to less decimal precision

open
5
2005-08-03
2005-08-03
No

Section 7.3.2 in "Applied_DWS2_Programming"
indicates that "In DWS, all variables are initialized i.e. a
variable is defined immediately after the var-statement is
executed."

This certainly appears to be the case with an explicit
initialization, but not an implicit one. An expression
containing an implicitly-initialized float variable seems to
perform an unexpected and undesired type cast to a
Currency type, resulting in a loss of decimal precision in
the assigned variable. Examples:

{ This works as expected }
var s1: float = 0;
s1 := s1 + pi / 2;
ShowMessage( FloatToStr( s1 ) );

{ s2 not initialized to zero: Type cast to Currency with 4
decimals? }
var s2: float;
s2 := s2 + pi / 2;
ShowMessage( FloatToStr( s2 ) );

{ same as s2, but uses a different uninitialized variable
in the expression }
var s3: float = 0;
var s4: float;
s3 := s4 + pi / 2;
ShowMessage( FloatToStr( s3 ) );

Discussion


Log in to post a comment.