Recent

Author Topic: Problem with Compare Value  (Read 4459 times)

Badger

  • Full Member
  • ***
  • Posts: 144
Problem with Compare Value
« on: June 27, 2020, 04:17:41 am »
Moving all my programs to new computer and have a problem with one of my Lazarus projects.  The following code gives the error:"Can't determine which overload function to call" on the CompareValue line although it worked on myu old machine.

Code: Pascal  [Select][+][-]
  1. function CompareTextAsCurrency: Integer;                        // Float
  2.           var c1,c2:Currency;
  3.           begin
  4.             if TryStrToCurr(Table.Cells[ACol,ARow],c1) and
  5.                TryStrToCurr(Table.Cells[BCol,BRow],c2) then
  6.             Result := CompareValue((c1), c2)
  7.             else Exit(0);
  8.           end;
 

I have moved from Lazarus v1.8.4  to 2.0.8 with the latest version of FPC

I have done some looking but can't find info for this error relevant to this particular situation
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with Compare Value
« Reply #1 on: June 27, 2020, 09:58:42 am »
There is no overload for comparevalue(curr,curr) so the compiler can't decide.
It may be an idea to file a bug report and ask for a comparevalue for currency.
I have submitted a patch based on the code below...
See https://www.freepascal.org/docs-html/rtl/math/comparevalue.html
and https://bugs.freepascal.org/view.php?id=37275

E.g:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses math;
  3. // this could be added to math and satisfies the convention used for the others
  4. function CompareValue(const A, B  : Currency): TValueRelationship;
  5. begin
  6.   result:=GreaterThanValue;
  7.   if a=b then
  8.     result:=EqualsValue
  9.   else
  10.    if a<b then
  11.      result:=LessThanValue;
  12. end;
  13. //
  14. var
  15.   a,b: currency;
  16. begin
  17.   a:= 1.2345;
  18.   b:= 2.3456;
  19.   writeln(comparevalue(a,b));
  20. end.
I am surprised this worked previously... are you sure?
Anyway, with the above code it works and the patch also works on debian and windows.
« Last Edit: June 27, 2020, 10:45:14 am by Thaddy »
Specialize a type, not a var.

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Problem with Compare Value
« Reply #2 on: June 27, 2020, 01:40:16 pm »
Yes it worked before (and still works - compiles and runs) on my old puter.  As I say, on the new puter, I have used  updated Lazarus, FPC and the latest version of Windows 10 although Windows used to keep itself up to date.
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Problem with Compare Value
« Reply #3 on: June 27, 2020, 03:32:36 pm »
Can it be that you used 32-bit Lazarus on the old computer and 64-bit on the new one?

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with Compare Value
« Reply #4 on: June 27, 2020, 07:05:37 pm »
Before I provided the patch I tested multiple bitness and platforms. The error occurred on all. The provided patch (based on above code) solves it on all.
Since currency is technically not a float type it is also the correct way to solve it (imho)
Although I can not explain that the error did not show up on win32 as ASerge reported.
In my analyses this is simply by accident.
« Last Edit: June 27, 2020, 07:24:23 pm by Thaddy »
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Problem with Compare Value
« Reply #5 on: June 28, 2020, 05:21:00 am »
Although I can not explain that the error did not show up on win32 as ASerge reported.
Very simple. Project:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. uses Math;
  4.  
  5. var
  6.   C1: Currency = 0;
  7.   C2: Currency = 0;
  8.   B: TValueRelationship;
  9. begin
  10.   B := CompareValue(C1, C2);
  11. end.

Compile it:
Quote
...\fpc\3.0.4\bin\i386-win32\fpc.exe "...\project1.lpr"
Free Pascal Compiler version 3.0.4 [2020/04/11] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling ...\project1.lpr
project1.lpr(8,3) Note: Local variable "B" is assigned but never used
Linking ...\project1.exe
12 lines compiled, 0.2 sec, 64256 bytes code, 4164 bytes data
1 note(s) issued
Win32 - OK

Quote
...\fpc\3.0.4\bin\x86_64-win64\fpc.exe "...\project1.lpr"
Free Pascal Compiler version 3.0.4 [2020/04/11] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win64 for x64
Compiling ...\project1.lpr
project1.lpr(10,8) Error: Can't determine which overloaded function to call
project1.lpr(13) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: ...\fpc\3.0.4\bin\x86_64-win64\ppcx64.exe returned an error exitcode
Win64 - not.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Problem with Compare Value
« Reply #6 on: June 28, 2020, 05:42:27 am »
No problem with macOS 10.14.6 in 32 bit or 64 bit.

Code: [Select]
$ /usr/local/lib/fpc/3.0.4/ppc386 program.pas
Free Pascal Compiler version 3.0.4 [2018/09/30] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Darwin for i386
Compiling program.pas
program.pas(8,3) Note: Local variable "B" is assigned but never used
Assembling (pipe) program.s
Linking program
ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
11 lines compiled, 0.2 sec
1 note(s) issued

Code: [Select]
$ /usr/local/lib/fpc/3.0.4/ppcx64 program.pas
Free Pascal Compiler version 3.0.4 [2018/09/30] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Darwin for x86_64
Compiling program.pas
program.pas(8,3) Note: Local variable "B" is assigned but never used
Assembling (pipe) program.s
Linking program
11 lines compiled, 0.1 sec
1 note(s) issued

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Problem with Compare Value
« Reply #7 on: June 28, 2020, 07:34:27 am »
...\fpc\3.0.4\bin\i386-win32\fpc.exe "...\project1.lpr"
Free Pascal Compiler version 3.0.4 [2020/04/11] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win32 for i386

Quote
...\fpc\3.0.4\bin\x86_64-win64\fpc.exe "...\project1.lpr"
Free Pascal Compiler version 3.0.4 [2020/04/11] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win64 for x64

From the above I noticed that your FPC 3.0.4 compilers are dated 2020/04/11, whereas mine are 2018/09/30.

A while ago someone else was having an issue and I noticed a similar date discrepancy - when they used the "official" builds, their inexplicable problems went away.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Problem with Compare Value
« Reply #8 on: June 28, 2020, 08:19:40 am »
From the above I noticed that your FPC 3.0.4 compilers are dated 2020/04/11, whereas mine are 2018/09/30.
A while ago someone else was having an issue and I noticed a similar date discrepancy - when they used the "official" builds, their inexplicable problems went away.
This is also the official version, only in the delivery of Lazarus 2.0.8 (just from April 2020).

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with Compare Value
« Reply #9 on: June 28, 2020, 09:12:06 am »
@ASerge
I hope you agree it would still be better to have an explicit overload for currency.
That does  not break already working platforms and fixes all others.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Problem with Compare Value
« Reply #10 on: June 28, 2020, 10:25:45 am »
I hope you agree it would still be better to have an explicit overload for currency.
Yes, but only Win64 needs it, as I understand it.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with Compare Value
« Reply #11 on: June 28, 2020, 02:25:03 pm »
Yes, but only Win64 needs it, as I understand it.
No. my raspberry pies needed it too (32 bit arm). As I wrote I could easily reproduce the issue. I am mostly working on those, not Windows.
But PascalDragon has solved the issue in a different way in trunk (tested 3.3.1-r45707)
My code has still value for those that do not use trunk, simply copy in the code from my example.
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problem with Compare Value
« Reply #12 on: June 28, 2020, 02:25:13 pm »
The added overload should be applicable to both because you do not want the compiler converting a currency over to a float to perform that operation. This defeats the whole reason why there Is a currency that uses the Int64 as the storage media.

 If you cast the currency to a Int64 the results are accurate for a compare of that nature, I always attempt to keep the float operations far away from it.


 I haven't checked the 32 bit port and I can't say which one gets currently called but if the compiler is calling a float operations, there by converting to a float from a currency then I really don't think that is the proper way to do it.

 I use currency types for fixed point math just to stay far away from floats to avoid the well known issues with floats

 That's my 2 cents worth and these days it seems it's not worth much.

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with Compare Value
« Reply #13 on: June 28, 2020, 03:24:05 pm »
Yes, I agree, basically you are right: a currency type should be derived from integer and scaled, but in FPC it is mapped to float types on some platforms.

Sven fixed the mapping to floats in trunk and they now work, but just in case you can use my solution on older versions of the compiler.
Specialize a type, not a var.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Problem with Compare Value
« Reply #14 on: June 28, 2020, 04:02:28 pm »
Hi!

The type currency is nothing but an Int64.
The last 4 digits are the fractional part.

So the range of the currency is from MaxInt64 / 10000 downto  -MaxInt64/10000

Winni
« Last Edit: June 28, 2020, 04:04:05 pm by winni »

 

TinyPortal © 2005-2018