Recent

Author Topic: A curious hint that was not supposed to exist...  (Read 2840 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
A curious hint that was not supposed to exist...
« on: August 19, 2017, 06:37:13 am »
This is probably not very important but it's bugging me.

I've got the following bit of code where all three variables are of type Currency.
Code: Pascal  [Select][+][-]
  1.    aCurrency1 := aCurrency2 / aCurrency3;
  2.  

The compiler is giving me the following hint:
Quote
Hint: use DIV instead to get an integer result

Now, why would it tell me to use div on currency? I checked the free Pascal wiki and the Lazarus wiki and the Delphi stuff and from what I've read it seems that Currency is a floating point type.

I'd like to add that the actual currency values are within a variant record with integer and cardinal type as well but that should not have any bearing on this now, would it?
Thoughts?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: A curious hint that was not supposed to exist...
« Reply #1 on: August 19, 2017, 08:04:01 am »
The hint is a hint and it is not correct can be ignored.
However, currency is represented as a 64 bits fixed point floating point type with 4 decimal places precisioninternally it is an integer 64 type with scaling by 10.000 applied to represent a final fixed point 64 bits float with 4 decimal precision.
I suspect the hint is thrown by the compiler at a point where it performs the internal integer calculations.
« Last Edit: August 19, 2017, 08:17:07 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: A curious hint that was not supposed to exist...
« Reply #2 on: August 19, 2017, 11:10:56 am »
Based on your explanations (Thanks by the way  :)), I wondered if for Currency, / and div would be equivalent. So I wrote this bit of code.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var C1, C2, C3, C4: Currency;
  3. var E1, E2, E3, E4: Extended;
  4. begin
  5.   C1 := 12.1212   ;
  6.   C2 :=  3.33     ;
  7.   C3 := C1 / C2   ; //that should be 3.34;
  8.   C4 := C1 div C2 ; //what could that be?
  9.   Writeln('C3 = ', C3);
  10.   Writeln('C4 = ', C4);
  11.   E1 := 12.1212   ;
  12.   E2 :=  3.33     ;
  13.   E3 := E1 / E2   ; //that should be 3.34;
  14.   //Next line requires typecast to work...
  15.   //uncomment to see the compiler's error.
  16.   //E4 := E1 div E2 ; //what could that be?
  17.   Writeln('E3 = ', E3);
  18.   Writeln('E4 = ', E4);
  19.   Readln;
  20. end.
  21.  

As expected, they're not. But what's interesting is that the compiler does not flag the statement C1 div C2 as an error the way it flags the commented statement E4 := E1 div E2. I guess that the type protection for Currency is not so airtight that it would prevent integer operations from executing properly?

I suppose that's part of the compatibility of currency with integers? I did not think that these two types were that compatible.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: A curious hint that was not supposed to exist...
« Reply #3 on: August 19, 2017, 12:56:29 pm »
Well,
This is probably not very important but it's bugging me.

I've got the following bit of code where all three variables are of type Currency.
Code: Pascal  [Select][+][-]
  1.    aCurrency1 := aCurrency2 / aCurrency3;
  2.  

The compiler is giving me the following hint:
Quote
Hint: use DIV instead to get an integer result

Now, why would it tell me to use div on currency? I checked the free Pascal wiki and the Lazarus wiki and the Delphi stuff and from what I've read it seems that Currency is a floating point type.

I'd like to add that the actual currency values are within a variant record with integer and cardinal type as well but that should not have any bearing on this now, would it?
Thoughts?
It is clearly documented and it is exactly what I wrote. It is a fixed point float type with a  internal to the compiler  scaled integer representation. The scaling factor is 10.000.
https://www.freepascal.org/docs-html/current/ref/refsu5.html#x27-300003.1.2
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Currency
https://www.safaribooksonline.com/library/view/delphi-in-a/1565926595/re56.html

You are clearly in the habit of not reading very well and not reading the correct docs at all..

I was actually very accurate from the top of my head... In your second example, you forgot to scale first.
« Last Edit: August 19, 2017, 01:01:21 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: A curious hint that was not supposed to exist...
« Reply #4 on: August 19, 2017, 09:45:11 pm »
Before upbraiding me on what read and what I did not, take the time to read what I wrote and quote the right part of my message.

You've got a wealth of knowledge but a very short fuse. You tell me that I'm in the habit of not reading, well straight back at you! Did you actually take the time to read what I wrote?

I was merely pointing out the difference in the way div works between currency and extended. The fact that _internally_ the compiler chooses int64 to implement a floating point type such as currency should be irrelevant and programmers should not rely on this knowledge because it can change tomorrow. The compiler seems to surface this when allowing this statement Currency1:= Currency2 div Currency 3 to compile because it is allowing a div operation to act on what is declared as a floating point type.

What's so difficult to understand and what got you so ticked off?

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: A curious hint that was not supposed to exist...
« Reply #5 on: August 20, 2017, 08:43:15 pm »
I do believe it is just a helpful hint letting you know, the programmer, as to what is
really happening in the background.

 The currency is not a floating point storage media. It is just a int64 type.

 You can in theory use the DIV and the results should be the same, but it'll actually
 operate faster in code. I used an old version of FPC and I had to cast to a Int64 to use
the DIV, but the results were the same except using binary math instead.
 
 Using /  allows you to specify fractions in the division and more code in the
background.

 The hint is there to let you know you can use DIV for non fraction divides for performance
of code if you wish too. Maybe the Hint should be worded differently. ?

The only true wisdom is knowing you know nothing

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: A curious hint that was not supposed to exist...
« Reply #6 on: August 22, 2017, 04:58:17 pm »
Thanks Jamie, for the clarification.

Yes, I do agree that better wording would have helped. In my opinion, If the hint said
Quote
"For a currency division without remainder, DIV is faster than the standard division operator '/'
Then the purpose of the hint would have been clearer. That's just me.



 

 

TinyPortal © 2005-2018