Recent

Author Topic: Mod problem????, I  (Read 3893 times)

apexcol

  • Jr. Member
  • **
  • Posts: 54
Mod problem????, I
« on: August 28, 2017, 02:28:25 pm »
Hi, I'm trying to make a multi-lingual function to parse a number to letters: 256 >> 'DOSCIENTOS CINCUENTA Y SEIS'.  'TWO HUNDRED FIFTY SIX'.

I started to put some code and there is a problem with mod.

Code: Pascal  [Select][+][-]
  1. function NumToLetters(numer: integer): String;
  2. var
  3.   L, N: Integer;
  4.   U: integer;
  5.   D: integer;
  6.   C: Integer;
  7. begin
  8.   result := '';
  9.   N := 0;
  10.   U := 0;
  11.   D := 0;
  12.   C := 0;
  13.   if numer > 0 then begin
  14.     N := numer;
  15.     U := N mod 10;
  16.     D := N mod 100;
  17.     C := N mod 1000;
  18.  
  19.     result := 'notYetDefined';  /// When I debug up to here, it shows U as 1000!!!!
  20.   end
  21.   else
  22.     result := 'cero'
  23. end;
  24.  

debugging this code it shows the following results on variables...
U: 1000;
D: 56;
C: 256;
N: 1000;

What is the problem?

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: Mod problem????, I
« Reply #1 on: August 28, 2017, 02:52:07 pm »
What is the problem?
For me it works fine.

But did you turn off compiler optimalisation?

After the mod-lines you don't use N, U, D and C anymore. So why would the program keep these values.

If you really want to know if they have proper values do this:
Code: Pascal  [Select][+][-]
  1.     N := numer;
  2.     U := N mod 10;
  3.     D := N mod 100;
  4.     C := N mod 1000;
  5.     Showmessage('N = ' + IntToStr(N) + ' U = ' + IntToStr(U) + ' D = ' + IntToStr(D) + ' C = ' + IntToStr(C));

What is the result from the ShowMessage then ??

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Mod problem????, I
« Reply #2 on: August 28, 2017, 03:02:43 pm »
Oh, for God's Sake!!!!

Next time I take photos!!!  Now it works!!!

And I swear I recompiled so many times, even rebooted and changed memory!!!

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1186
Re: Mod problem????, I
« Reply #3 on: August 28, 2017, 03:13:55 pm »
Hi, I'm trying to make a multi-lingual function to parse a number to letters: 256 >> 'DOSCIENTOS CINCUENTA Y SEIS'.  'TWO HUNDRED FIFTY SIX'.

Take a look below to see if one of them might help you:
Convierte numeros a letras de forma recursiva
https://clubdelphi.com/foros/showthread.php?p=478043

how to convert number to words
http://www.delphipages.com/forum/archive/index.php/t-33856.html

How to convert Currency to string
http://www.delphigroups.info/2/7/221490.html
« Last Edit: August 29, 2017, 07:13:51 am by valdir.marcos »

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Mod problem????, I
« Reply #4 on: August 29, 2017, 01:18:07 am »
I see that when you debug, sometimes it changes the values, so I decided to implement an assembler function, but I don't know how to change the MOD implementation, or add as overrided function.

Code: Pascal  [Select][+][-]
  1. function FMOD(X, Y: Int64): Int64; assembler;
  2. asm
  3. {$IFDEF CPUX64}
  4.     mov     rcx, rsi;
  5.     mov     rax, rdi;
  6.     div     rcx;
  7.     mov     rax, rdx;
  8. {$ELSE}
  9.     mov     edx, edx;
  10.     mov     ecx, edi;
  11.     div     ecx;
  12.     mov     eax, edx;
  13. {$ENDIF}
  14. end;
  15.  

I don't have 32 bit system, so I couldn't try it, anybody can try and put the results?

Mick

  • Jr. Member
  • **
  • Posts: 51
Re: Mod problem????, I
« Reply #5 on: August 29, 2017, 01:43:34 am »
You are taking the sledgehammer to crack a nut.
The plain mod operator is working fine, you don't need the assembly code for that.
Read what the others said about the compiler optimizations and elimination of the unused variables.

Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: Mod problem????, I
« Reply #6 on: August 30, 2017, 12:46:17 pm »
Besides, he should have used const x,y.
So the code is not portable and potentially worse than the fmod function in math and also worse than the current overloaded operators.
And fmod is fmod: floating point modulo.... So it is wrong anyway. Examine the math unit and check what the compiler actually generates for you. You will be surprised.
Note that simple mod works also in FPC (not in delphi) with floats.
Also note that you need integer mod, not fmod...
« Last Edit: August 30, 2017, 03:04:44 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018