Recent

Author Topic: ABS truncates a Variant float ?  (Read 8275 times)

jamie

  • Hero Member
  • *****
  • Posts: 6077
ABS truncates a Variant float ?
« on: February 25, 2018, 02:53:55 am »
maybe I am losing it but this works fine in Delphi..

Var
 V:Variant;
Begin
 V := -34.5;
 Writeln(V); //Shows correctly.

 V :=Abs(V);
 Writeln(V);
  34
   what happen to my .5 ?

 I can do this and it works
  if V < 0 then V := -V;
 
 Fpc 3.0.4
The only true wisdom is knowing you know nothing

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: ABS truncates a Variant float ?
« Reply #1 on: February 25, 2018, 03:54:30 am »
it seems that a cast solves your problem
Code: [Select]
V :=Abs(single(V));
Which in itself seems to indicate that the wrong overload is used by default ? (even though varisfloat indicates that it is dealing with a float-value)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ABS truncates a Variant float ?
« Reply #2 on: February 25, 2018, 04:16:01 am »
maybe I am losing it but this works fine in Delphi..

Var
 V:Variant;
Begin
 V := -34.5;
 Writeln(V); //Shows correctly.

 V :=Abs(V);
 Writeln(V);
  34
   what happen to my .5 ?
confirmed on fpc 3.0.2. I consider this is a bug.
I can do this and it works
  if V < 0 then V := -V;
 
 Fpc 3.0.4
not confirmed it produces the same results as the call to abs for me.

In any case a bug report would be appropriate at this point.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: ABS truncates a Variant float ?
« Reply #3 on: February 25, 2018, 04:19:55 am »
Feel free to follow up, I kind of get beat on when ever I find something  :(
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ABS truncates a Variant float ?
« Reply #4 on: February 25, 2018, 04:24:47 am »
Feel free to follow up, I kind of get beat on when ever I find something  :(
so is everyone else. My success rate of bug reporting is around 20%.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: ABS truncates a Variant float ?
« Reply #5 on: February 25, 2018, 04:34:42 am »
@taazz
sorry. my bad i removed my initial post because i did something stupid (not had my cuppa tea, perhaps you forgot yours also).

To make sure:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   variants;
  7.  
  8. procedure Test1;
  9. Var
  10.   V:Variant;
  11. Begin
  12.   V := -34.5;
  13.   if VarIsFloat(V) then WriteLn('isfloat') else WriteLn('No floating boat');
  14.   WriteLn(V); //Shows correctly.
  15.  
  16.   V :=Abs(V);
  17.   if VarIsFloat(V) then WriteLn('isfloat') else WriteLn('No floating boat');
  18.   Writeln(V);  //  34   what happen to my .5 ?
  19. end;
  20.  
  21. procedure Test2;
  22. Var
  23.   V:Variant;
  24. Begin
  25.   V := -34.5;
  26.   if VarIsFloat(V) then WriteLn('isfloat') else WriteLn('No floating boat');
  27.   WriteLn(V); //Shows correctly.
  28.  
  29.   if V < 0 then V := -V;
  30.   if VarIsFloat(V) then WriteLn('isfloat') else WriteLn('No floating boat');
  31.   WriteLn(V); //Shows correctly.
  32. end;
  33.  
  34. procedure Test3;
  35. Var
  36.   V:Variant;
  37. Begin
  38.   V := -34.5;
  39.   if VarIsFloat(V) then WriteLn('isfloat') else WriteLn('No floating boat');
  40.   WriteLn(V); //Shows correctly.
  41.  
  42.   V :=Abs(ValReal(V));
  43.   if VarIsFloat(V) then WriteLn('isfloat') else WriteLn('No floating boat');
  44.   Writeln(V); //Shows correctly.
  45. end;
  46.  
  47. begin
  48.   Test1;
  49.   WriteLn;
  50.   Test2;
  51.   WriteLn;
  52.   Test3;
  53. end.
  54.  

produces for me:

Code: [Select]
isfloat
-34,5
No floating boat
34

isfloat
-34,5
isfloat
34,5

isfloat
-34,5
isfloat
34,5
3.0.4, 3.0.2, 3.0.0 and 2.6.4

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ABS truncates a Variant float ?
« Reply #6 on: February 25, 2018, 04:49:35 am »
@taazz
sorry. my bad i removed my initial post because i did something stupid (not had my cuppa tea, perhaps you forgot yours also).
true even after I executed your test2 procedure I kept reading 34 instead of 34,5 although the line above was saying isfloat, I was thinking no its not!  :-[
None the less, this is a bug, or at least a unexpected behaviour that deserves a report and let the fpc team decide if it needs attention.
« Last Edit: February 25, 2018, 04:56:00 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: ABS truncates a Variant float ?
« Reply #7 on: February 25, 2018, 05:01:36 am »
for the time being I have done this
Code: Pascal  [Select][+][-]
  1. Function ABS(V:Variant):Variant; Overload; inline;
  2. Begin
  3.   If VarIsFloat(V) Then// Correction VarIsNumeric(V)...
  4.   if V< 0 Then result := -V else Result := V;
  5. end;                                
« Last Edit: February 25, 2018, 05:06:57 am by jamie »
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ABS truncates a Variant float ?
« Reply #8 on: February 25, 2018, 05:06:53 am »
for the time being I have done this
Code: Pascal  [Select][+][-]
  1. Function ABS(V:Variant):Variant; Overload; inline;
  2. Begin
  3.   If VarIsFloat(V) Then
  4.   if V< 0 Then result := -V else Result := V;
  5. end;                                
remove the varisfloat check and you are set maybe a VarIsNumeric to avoid numeric calculation on non numeric types.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: ABS truncates a Variant float ?
« Reply #9 on: February 25, 2018, 05:07:30 am »
Yes, I saw that, but I think I beat you to it ;)
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ABS truncates a Variant float ?
« Reply #10 on: February 25, 2018, 05:09:19 am »
Yes, I saw that, but I think I beat you to it ;)
Code: Pascal  [Select][+][-]
  1. Function ABS(V:Variant):Variant; Overload; inline;
  2. Begin
  3.   result := v;
  4.   If VarIsNumeric(V) Then
  5.     if V< 0 Then result := -v;
  6. end;  
:D
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: ABS truncates a Variant float ?
« Reply #11 on: February 25, 2018, 05:16:27 am »
I was actually thinking of raising and exception if anything other than a valid entry is
made there.
 
 it only makes sense because I don't think you would be using that on other Variant types  :D
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: ABS truncates a Variant float ?
« Reply #12 on: February 25, 2018, 06:02:55 am »
I was actually thinking of raising and exception if anything other than a valid entry is
made there.
 
 it only makes sense because I don't think you would be using that on other Variant types  :D
a valid option and probably a better choice.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14160
  • Probably until I exterminate Putin.
Re: ABS truncates a Variant float ?
« Reply #13 on: February 25, 2018, 11:15:45 am »
This works for me in trunk:
Code: Pascal  [Select][+][-]
  1. {uses variants, math}
  2. Function Abs(V:Variant):Variant; Overload; inline;
  3. Begin
  4.   Result := VarNull;
  5.   If VarIsNumeric(V) Then
  6.     Result := v * Sign(v);
  7. end;
Indeed the standard variant routine truncates. Also in trunk. Means a bug in variant numeric evaluation order. It should test for frac before assuming an integer type.
 
« Last Edit: February 25, 2018, 01:17:39 pm by Thaddy »
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: ABS truncates a Variant float ?
« Reply #14 on: February 25, 2018, 12:24:06 pm »
I asked on fpc-devel list.

Bart

 

TinyPortal © 2005-2018