1
r:=2.1;
while r <> 4.3 do
begin
    r:= r + 0.1;
    writeln(r);
end;

Where the r is real type, but it just won't work and prints ´r´ in scientific notification.

I dont know, maybe this is compiler rounding error or something like that I am beginner with pascal (actually forced to work with it what I honestly hate)

the output is enter image description here

Ideone link: http://ideone.com/fY5AYM

Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
  • 4
    What exactly is the desired behaviour? The loop condition seems problematic, as it demands equality of floating point values, which might not work as expected. If rounding is unfortunate, you might just jump beyond the desired limit value of `4.3`. – Codor Jan 09 '15 at 12:16
  • 2
    Read this http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html. – TLama Jan 09 '15 at 12:17
  • Possible duplicate of http://stackoverflow.com/questions/16550401/counting-the-number-of-decimal-places-in-pascal – Fernando Jan 09 '15 at 12:20
  • I think that this while loop should execute like from 2.1 to 4.3 with step 0.1, but this is not workin, I will post a screnshot – Bartłomiej Sobieszek Jan 09 '15 at 12:23
  • 1
    That's the typical problem with floating point. As @TLama commented, you will never get exact numbers with that data type. That's why `decimal` types were made. They take up more space but have an exact value. Here you can see that the same happens in Javascript, for instance: http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem – Andrew Jan 09 '15 at 12:32
  • 1
    Google *What every programmer should know about floating point math*. – Ken White Jan 09 '15 at 14:10
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – tmyklebu Jan 09 '15 at 20:50

2 Answers2

2

If you want to have exact decimals in Pascal, you have to use the currency type.

program Zadanie2;
var r : currency;
begin
    r:=2.1;
    while r <> 4.3 do
    begin
        r:= r + 0.1;
        writeln(r:0:2);
    end;
end.

Now the output ends at 4.30.

As you can notice, I also changed the format so you don't get the scientific notation.

Code and results: http://ideone.com/Hf65v2

Andrew
  • 7,602
  • 2
  • 34
  • 42
1

I fixed it by replacing

while r <> 4.3

with

while r < 4.4

But still, it's not a real solution

I guess there is nothing I can do with compiler

Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
  • It seems the closest to a `decimal` data type in Pascal is `currency`. Try your original code making `r` of that type. – Andrew Jan 09 '15 at 12:35
  • The `while r < 4.4` is what I would have suggested (+1) for a general float solution. As others have noted in comments, checking for strict equality or strict inequality (the direct complement) is problematic when using floats due to internal precision variances. When dealing with currency specifically (or anything that looks like currency), then the `currency` solution is nice. – lurker Jan 09 '15 at 17:46
  • You are using floating point, which simply can not represent most decimal values exactly. This is not a compiler problem ,it is inherent to floating point in ANY language. To avoid this, use Currency or a special (third party) decimal type. – Rudy Velthuis Jan 25 '15 at 19:00