`Decimal` type and dec()

I was pleasantly surprised to discover that the dec() method can accept types other than integers/ordinals like Delphi/Object Pascal. I wrote the following code in several languages to test the BigDecimal and Decimal types by decrementing to 0 using Real numbers. As you know Float and Double types are not accurate enough to reach absolute 0:

namespace DecimalType_Oxy;

begin
  var x: Decimal := 1.0;   // C#: var x = 1.0m; 'm' indicates 'Decimal' type
  
  const decr: Decimal = 0.1;
  const zero: Decimal = 0.0;

    while x ≥ zero do
    begin
      writeLn(x);
      x := x - decr;  
    end;
end.

It works as expected with the following output:

1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0.0

But I thought I might attempt to implement more idiomatic Pascal syntax by using the dec() method instead:

namespace DecimalType_Oxy;

begin
  var x: Decimal := 1.0; 
  
  const decr: Decimal = 0.1;
  const zero: Decimal = 0.0;

    while x ≥ zero do
    begin
      writeLn(x);
      dec(x, decr); 
    end;
end.

But this version will lock up the Fire IDE and must be force quit. So, is it reasonable to assume that the dec() method probably does not accept the Decimal type? Thanks for looking! :cowboy_hat_face:

Reverting to a smaller program which shows that the y variable is never read when using the dec() method. The program compiles but with the unexpected output of 5.5. The statement x := x - y; works as expected.

So it appears the original code is outputting 1.0 in an endless while loop, since 0 is never reached.

Hm, even if it didn’t, it should not lock up the IDE. what platform was this, can you are your project?

—marc

1 Like

Ah yes. and reproduced. Fire really does not like 500k messages being dumped to the console log at once. I’ll need to find a good solution to prevent it locking up on that, w/o actually allowing the debug log to grow, literally, infinite…

I’ll log an issue to fix that and one for dec() – if it doesnt work with Decimal, it should not compile.

1 Like

Logged as bugs://E26898: dec() does not work with Decimal type

1 Like

Logged as bugs://E26899: Fire: infinite/recursive debug output crashes/hangs IDE

1 Like

Thanks for looking Marc! As an aside, have you and/or your team members ever considered adopting the m literal suffix for Oxygene, Mercury… allowing the compiler to infer the type from m vs explicit type annotation?

So we could write:
var num1 := 1.23m
versus
var num1:Decimal := 1.23; or var num1 := Decimal(1.23);

In some scenarios, 1.0m and 0.0m could effectively replace accessing fields Decimal.one and Decimal.zero.
:cowboy_hat_face:

Hm, we have not. it does not seem very Pascal-like, but someting worth considering; I’ll bring it up with the team.

1 Like

Not to sway you all one way or another, but the computer scientist university professors who created the cousin CLI-based Pascal is seriously considering adopting the m suffix for the Decimal type. :nerd_face: :point_up:

I just wrote this and I thought to myself.

  writeLn(Decimal.Round(Decimal(-2.54)));

Wouldn’t it be nicer if I could write it this way instead?

  writeLn(Decimal.Round(-2.54m));

:upside_down_face:

But wait, there’s more! Delphi’s %m format specifier maps nicely to the m literal suffix for decimal types in C#. :nerd_face: :+1:

no arguent that a shorter syntax woukld be nice. but a letter suffix is orthogonal too how all other Pascal numerals work, and its a straight lift from C(#).

1 Like

2909 has some mitigations for “too much logging”. but endless too-fast logging will still make you sad. though chances are if you press Stop it will react eventually, now, rather than never. A proper fix will probably need cooperation with the debug engine itself.

Looks work on this is ongoing today, so probably will be in coming Friday’s build.

1 Like

See … this is what happens, Marc, when you try and do the right thing and maintain Pascal constructs like dec() and succ() in Oxygene instead of going with .Net syntax: -= and +=

You know I’m joking. :upside_down_face:

:joy:.

id actually like to see support += and -= at some point (especially since w have them anyways, for events). just haven’t gotten around to logging that yet…

1 Like

bugs://E26898 was closed as fixed.

1 Like

So, is this fix mainly to prevent an endless loop? I tested the use of dec() with the Decimal type and although the code compiles, the output is not as expected. It looks like the y variables is never read and simply bypassed?:

namespace DecimalDec_Oxy;

begin
  var x: Decimal := 5.5;
  writeLn(x.GetType); // System.Decimal 
  var y: Decimal := 0.5;
  dec(x, y); // so 'y' is never read
  writeLn(x); // 5.5
  
  var x1: Decimal := 5.5;
  var y1: Decimal := 0.5;
  x1 := x1 - y1;
  writeLn(x1); // 5.0 
end.

No, this fix implements dec() properly, but it’s also not out yet? it’s in 2911, out later today.

1 Like

Confirmed, it works as expected. Thank you Marc and team members! The Object Pascal devotees especially thank you all! :nerd_face: :+1:

Happy to hear!!

1 Like