Going nuts here...

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)


Comments

  • : 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.
  • : : 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

  • : : : 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!
    :-)




  • : : : : 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.
  • : : : : : 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 !

  • : : : : : : 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.
  • : : : : : : : 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 ?

  • : : : : : : : : 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:
    [code]
    rsum := Round(rsum, 2);
    [/code]
    where rsum can be any floating point variable (like real, double, extended, complex, etc. etc.).
  • : : : : : : : : : 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:
    : [code]
    : rsum := Round(rsum, 2);
    : [/code]
    : where rsum can be any floating point variable (like real, double, extended, complex, etc. etc.).
    :

    Thanks, I'll try it out :-)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories