## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14016

This Forum Only

Going nuts here... Posted by Luke_Skywalker on 7 Apr 2007 at 11:11 AM
Hi Guys !

I've become caught in a strange problem.

- yet it's very simple i guess :

Var r1, r2, r3, rsum : REAL;

...

begin
r1:=6000.20;
r2:=400.20;
r3:=100.20;
rsum:=r1+r2+r3;
end;

...

- I would expect rsum to be nothing but 6500.60
Though it keeps claiming the result to be 6500,59999999999...

..... help .... anyone ?

(Pascal compiler=Freepascal 1.0.6)

Re: Going nuts here... Posted by zibadian on 7 Apr 2007 at 1:28 PM
: Hi Guys !
:
: I've become caught in a strange problem.
:
: - yet it's very simple i guess :
:
:
: Var r1, r2, r3, rsum : REAL;
:
: ...
:
: begin
: r1:=6000.20;
: r2:=400.20;
: r3:=100.20;
: rsum:=r1+r2+r3;
: end;
:
: ...
:
: - I would expect rsum to be nothing but 6500.60
: Though it keeps claiming the result to be 6500,59999999999...
:
: ..... help .... anyone ?
:
: (Pascal compiler=Freepascal 1.0.6)
:
:
:
It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
Re: Going nuts here... Posted by Phat Nat on 7 Apr 2007 at 1:40 PM
: : Hi Guys !
: :
: : I've become caught in a strange problem.
: :
: : - yet it's very simple i guess :
: :
: :
: : Var r1, r2, r3, rsum : REAL;
: :
: : ...
: :
: : begin
: : r1:=6000.20;
: : r2:=400.20;
: : r3:=100.20;
: : rsum:=r1+r2+r3;
: : end;
: :
: : ...
: :
: : - I would expect rsum to be nothing but 6500.60
: : Though it keeps claiming the result to be 6500,59999999999...
: :
: : ..... help .... anyone ?
: :
: : (Pascal compiler=Freepascal 1.0.6)
: :
: :
: :
: It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
:

It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
Just one of those weird things that a computer does differently from our thought patterns.

Phat Nat

Re: Going nuts here... Posted by Luke_Skywalker on 7 Apr 2007 at 2:42 PM
: : : Hi Guys !
: : :
: : : I've become caught in a strange problem.
: : :
: : : - yet it's very simple i guess :
: : :
: : :
: : : Var r1, r2, r3, rsum : REAL;
: : :
: : : ...
: : :
: : : begin
: : : r1:=6000.20;
: : : r2:=400.20;
: : : r3:=100.20;
: : : rsum:=r1+r2+r3;
: : : end;
: : :
: : : ...
: : :
: : : - I would expect rsum to be nothing but 6500.60
: : : Though it keeps claiming the result to be 6500,59999999999...
: : :
: : : ..... help .... anyone ?
: : :
: : : (Pascal compiler=Freepascal 1.0.6)
: : :
: : :
: : :
: : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: :
:
: It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: Just one of those weird things that a computer does differently from our thought patterns.
:
: Phat Nat
:
:
Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!

Re: Going nuts here... Posted by zibadian on 7 Apr 2007 at 6:25 PM
: : : : Hi Guys !
: : : :
: : : : I've become caught in a strange problem.
: : : :
: : : : - yet it's very simple i guess :
: : : :
: : : :
: : : : Var r1, r2, r3, rsum : REAL;
: : : :
: : : : ...
: : : :
: : : : begin
: : : : r1:=6000.20;
: : : : r2:=400.20;
: : : : r3:=100.20;
: : : : rsum:=r1+r2+r3;
: : : : end;
: : : :
: : : : ...
: : : :
: : : : - I would expect rsum to be nothing but 6500.60
: : : : Though it keeps claiming the result to be 6500,59999999999...
: : : :
: : : : ..... help .... anyone ?
: : : :
: : : : (Pascal compiler=Freepascal 1.0.6)
: : : :
: : : :
: : : :
: : : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: : :
: :
: : It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: : Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: : Just one of those weird things that a computer does differently from our thought patterns.
: :
: : Phat Nat
: :
: :
: Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!
:
:
:
:
:
:
The best solution is to format the number while showing it. Since most numbers often have a limited number of significat decimals, you should only show those. In this case it is 2 decimal digits.
Re: Going nuts here... Posted by Luke_Skywalker on 8 Apr 2007 at 2:48 AM
: : : : : Hi Guys !
: : : : :
: : : : : I've become caught in a strange problem.
: : : : :
: : : : : - yet it's very simple i guess :
: : : : :
: : : : :
: : : : : Var r1, r2, r3, rsum : REAL;
: : : : :
: : : : : ...
: : : : :
: : : : : begin
: : : : : r1:=6000.20;
: : : : : r2:=400.20;
: : : : : r3:=100.20;
: : : : : rsum:=r1+r2+r3;
: : : : : end;
: : : : :
: : : : : ...
: : : : :
: : : : : - I would expect rsum to be nothing but 6500.60
: : : : : Though it keeps claiming the result to be 6500,59999999999...
: : : : :
: : : : : ..... help .... anyone ?
: : : : :
: : : : : (Pascal compiler=Freepascal 1.0.6)
: : : : :
: : : : :
: : : : :
: : : : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: : : :
: : :
: : : It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: : : Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: : : Just one of those weird things that a computer does differently from our thought patterns.
: : :
: : : Phat Nat
: : :
: : :
: : Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!
: :
: :
: :
: :
: :
: :
: The best solution is to format the number while showing it. Since most numbers often have a limited number of significat decimals, you should only show those. In this case it is 2 decimal digits.
:

Yes, I see your point (no pun intended )
As it is, this small examaple of my problem is just a very small fragment taken out of a much bigger procedure, where the rsum-variable is used for many further calculations. They all come out wrong, to some extend, when the rsum is inaccurate.
I found a way of dealing with this, but I fear that I might have solved the problem in a rather unusual and mayby...clumbsy way.
To make a long story short, what I've done (in principle) is to convert the real-variable into a string, and in the same run limit the numbers of digits to two decimals. Then, convert it back to a real-variable again.
It works ...
- But maybe there are other, more brilliant ways of dealing with this...

Greetings !

Re: Going nuts here... Posted by zibadian on 8 Apr 2007 at 2:59 AM
: : : : : : Hi Guys !
: : : : : :
: : : : : : I've become caught in a strange problem.
: : : : : :
: : : : : : - yet it's very simple i guess :
: : : : : :
: : : : : :
: : : : : : Var r1, r2, r3, rsum : REAL;
: : : : : :
: : : : : : ...
: : : : : :
: : : : : : begin
: : : : : : r1:=6000.20;
: : : : : : r2:=400.20;
: : : : : : r3:=100.20;
: : : : : : rsum:=r1+r2+r3;
: : : : : : end;
: : : : : :
: : : : : : ...
: : : : : :
: : : : : : - I would expect rsum to be nothing but 6500.60
: : : : : : Though it keeps claiming the result to be 6500,59999999999...
: : : : : :
: : : : : : ..... help .... anyone ?
: : : : : :
: : : : : : (Pascal compiler=Freepascal 1.0.6)
: : : : : :
: : : : : :
: : : : : :
: : : : : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: : : : :
: : : :
: : : : It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: : : : Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: : : : Just one of those weird things that a computer does differently from our thought patterns.
: : : :
: : : : Phat Nat
: : : :
: : : :
: : : Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!
: : :
: : :
: : :
: : :
: : :
: : :
: : The best solution is to format the number while showing it. Since most numbers often have a limited number of significat decimals, you should only show those. In this case it is 2 decimal digits.
: :
:
: Yes, I see your point (no pun intended )
: As it is, this small examaple of my problem is just a very small fragment taken out of a much bigger procedure, where the rsum-variable is used for many further calculations. They all come out wrong, to some extend, when the rsum is inaccurate.
: I found a way of dealing with this, but I fear that I might have solved the problem in a rather unusual and mayby...clumbsy way.
: To make a long story short, what I've done (in principle) is to convert the real-variable into a string, and in the same run limit the numbers of digits to two decimals. Then, convert it back to a real-variable again.
: It works ...
: - But maybe there are other, more brilliant ways of dealing with this...
:
: Greetings !
:
:
I would use a Round() function for that. There are some, which allow you to specify the number of digits.
Re: Going nuts here... Posted by Luke_Skywalker on 8 Apr 2007 at 3:35 AM
: : : : : : : Hi Guys !
: : : : : : :
: : : : : : : I've become caught in a strange problem.
: : : : : : :
: : : : : : : - yet it's very simple i guess :
: : : : : : :
: : : : : : :
: : : : : : : Var r1, r2, r3, rsum : REAL;
: : : : : : :
: : : : : : : ...
: : : : : : :
: : : : : : : begin
: : : : : : : r1:=6000.20;
: : : : : : : r2:=400.20;
: : : : : : : r3:=100.20;
: : : : : : : rsum:=r1+r2+r3;
: : : : : : : end;
: : : : : : :
: : : : : : : ...
: : : : : : :
: : : : : : : - I would expect rsum to be nothing but 6500.60
: : : : : : : Though it keeps claiming the result to be 6500,59999999999...
: : : : : : :
: : : : : : : ..... help .... anyone ?
: : : : : : :
: : : : : : : (Pascal compiler=Freepascal 1.0.6)
: : : : : : :
: : : : : : :
: : : : : : :
: : : : : : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: : : : : :
: : : : :
: : : : : It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: : : : : Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: : : : : Just one of those weird things that a computer does differently from our thought patterns.
: : : : :
: : : : : Phat Nat
: : : : :
: : : : :
: : : : Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!
: : : :
: : : :
: : : :
: : : :
: : : :
: : : :
: : : The best solution is to format the number while showing it. Since most numbers often have a limited number of significat decimals, you should only show those. In this case it is 2 decimal digits.
: : :
: :
: : Yes, I see your point (no pun intended )
: : As it is, this small examaple of my problem is just a very small fragment taken out of a much bigger procedure, where the rsum-variable is used for many further calculations. They all come out wrong, to some extend, when the rsum is inaccurate.
: : I found a way of dealing with this, but I fear that I might have solved the problem in a rather unusual and mayby...clumbsy way.
: : To make a long story short, what I've done (in principle) is to convert the real-variable into a string, and in the same run limit the numbers of digits to two decimals. Then, convert it back to a real-variable again.
: : It works ...
: : - But maybe there are other, more brilliant ways of dealing with this...
: :
: : Greetings !
: :
: :
: I would use a Round() function for that. There are some, which allow you to specify the number of digits.
:
Sounds much simpler, but can I use the round-command with my real-variable rsum, and as a result get the rounded figure (including two digits right to the decimalpoint, meaning 6500.20) out in another real ?

Re: Going nuts here... Posted by zibadian on 8 Apr 2007 at 3:41 AM
: : : : : : : : Hi Guys !
: : : : : : : :
: : : : : : : : I've become caught in a strange problem.
: : : : : : : :
: : : : : : : : - yet it's very simple i guess :
: : : : : : : :
: : : : : : : :
: : : : : : : : Var r1, r2, r3, rsum : REAL;
: : : : : : : :
: : : : : : : : ...
: : : : : : : :
: : : : : : : : begin
: : : : : : : : r1:=6000.20;
: : : : : : : : r2:=400.20;
: : : : : : : : r3:=100.20;
: : : : : : : : rsum:=r1+r2+r3;
: : : : : : : : end;
: : : : : : : :
: : : : : : : : ...
: : : : : : : :
: : : : : : : : - I would expect rsum to be nothing but 6500.60
: : : : : : : : Though it keeps claiming the result to be 6500,59999999999...
: : : : : : : :
: : : : : : : : ..... help .... anyone ?
: : : : : : : :
: : : : : : : : (Pascal compiler=Freepascal 1.0.6)
: : : : : : : :
: : : : : : : :
: : : : : : : :
: : : : : : : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: : : : : : :
: : : : : :
: : : : : : It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: : : : : : Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: : : : : : Just one of those weird things that a computer does differently from our thought patterns.
: : : : : :
: : : : : : Phat Nat
: : : : : :
: : : : : :
: : : : : Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!
: : : : :
: : : : :
: : : : :
: : : : :
: : : : :
: : : : :
: : : : The best solution is to format the number while showing it. Since most numbers often have a limited number of significat decimals, you should only show those. In this case it is 2 decimal digits.
: : : :
: : :
: : : Yes, I see your point (no pun intended )
: : : As it is, this small examaple of my problem is just a very small fragment taken out of a much bigger procedure, where the rsum-variable is used for many further calculations. They all come out wrong, to some extend, when the rsum is inaccurate.
: : : I found a way of dealing with this, but I fear that I might have solved the problem in a rather unusual and mayby...clumbsy way.
: : : To make a long story short, what I've done (in principle) is to convert the real-variable into a string, and in the same run limit the numbers of digits to two decimals. Then, convert it back to a real-variable again.
: : : It works ...
: : : - But maybe there are other, more brilliant ways of dealing with this...
: : :
: : : Greetings !
: : :
: : :
: : I would use a Round() function for that. There are some, which allow you to specify the number of digits.
: :
: Sounds much simpler, but can I use the round-command with my real-variable rsum, and as a result get the rounded figure (including two digits right to the decimalpoint, meaning 6500.20) out in another real ?
:
:
That's the general idea of those functions. It wouldn't be useful to round an integer to a certain decimal point. I don't know freepascal so you might need to check the help files, but in various other languages there are round functions, which work like this:
```  rsum := Round(rsum, 2);
```

where rsum can be any floating point variable (like real, double, extended, complex, etc. etc.).
Re: Going nuts here... Posted by Luke_Skywalker on 8 Apr 2007 at 8:55 AM
: : : : : : : : : Hi Guys !
: : : : : : : : :
: : : : : : : : : I've become caught in a strange problem.
: : : : : : : : :
: : : : : : : : : - yet it's very simple i guess :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : : Var r1, r2, r3, rsum : REAL;
: : : : : : : : :
: : : : : : : : : ...
: : : : : : : : :
: : : : : : : : : begin
: : : : : : : : : r1:=6000.20;
: : : : : : : : : r2:=400.20;
: : : : : : : : : r3:=100.20;
: : : : : : : : : rsum:=r1+r2+r3;
: : : : : : : : : end;
: : : : : : : : :
: : : : : : : : : ...
: : : : : : : : :
: : : : : : : : : - I would expect rsum to be nothing but 6500.60
: : : : : : : : : Though it keeps claiming the result to be 6500,59999999999...
: : : : : : : : :
: : : : : : : : : ..... help .... anyone ?
: : : : : : : : :
: : : : : : : : : (Pascal compiler=Freepascal 1.0.6)
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : It is strange, but reals are know to show rounding errors. Yet it only should show up after the 8'th decimal or so. Perhaps casting the variables as doubles might do the trick.
: : : : : : : :
: : : : : : :
: : : : : : : It's because of the way that decimal numbers are stored. Think of a way an integer is stored as bits. The first bit is 1, ,second is 2, third is 4, fourth is 8, etc.
: : : : : : : Now when a decimal is stored, each bit represents .5 of the last bit, so bit 1 = 1/2 or 0.5, bit 2 = 1/4 or 0.25, bit 3 = 1/8 or 0.125, etc, so when you have real additions you can quited easily end up with a repeating number although it obviously (to us) adds to a whole number.
: : : : : : : Just one of those weird things that a computer does differently from our thought patterns.
: : : : : : :
: : : : : : : Phat Nat
: : : : : : :
: : : : : : :
: : : : : : Thanks guys. I didn't know what to think of it, but I'm actually feeling better now, knowing that there is a good reason for this behavior. I'll have a go at finding a suitable cure. Thanks again!
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : The best solution is to format the number while showing it. Since most numbers often have a limited number of significat decimals, you should only show those. In this case it is 2 decimal digits.
: : : : :
: : : :
: : : : Yes, I see your point (no pun intended )
: : : : As it is, this small examaple of my problem is just a very small fragment taken out of a much bigger procedure, where the rsum-variable is used for many further calculations. They all come out wrong, to some extend, when the rsum is inaccurate.
: : : : I found a way of dealing with this, but I fear that I might have solved the problem in a rather unusual and mayby...clumbsy way.
: : : : To make a long story short, what I've done (in principle) is to convert the real-variable into a string, and in the same run limit the numbers of digits to two decimals. Then, convert it back to a real-variable again.
: : : : It works ...
: : : : - But maybe there are other, more brilliant ways of dealing with this...
: : : :
: : : : Greetings !
: : : :
: : : :
: : : I would use a Round() function for that. There are some, which allow you to specify the number of digits.
: : :
: : Sounds much simpler, but can I use the round-command with my real-variable rsum, and as a result get the rounded figure (including two digits right to the decimalpoint, meaning 6500.20) out in another real ?
: :
: :
: That's the general idea of those functions. It wouldn't be useful to round an integer to a certain decimal point. I don't know freepascal so you might need to check the help files, but in various other languages there are round functions, which work like this:
:
```:   rsum := Round(rsum, 2);
: ```

: where rsum can be any floating point variable (like real, double, extended, complex, etc. etc.).
:

Thanks, I'll try it out

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic