# Board index » delphi » Problem with real numbers - labour.zip [0/1]

## Problem with real numbers - labour.zip [0/1]

Hi

I would like to write a program that takes as input the daily wage of a
labourer, determine the wage owed to him for a week's work, determine the
amount owed to the labourer for overtime work and then subtract any loan
the labourer may owe to the user from the amount owed to him.

I would, however, also like the program to tell the user the exact amount
of notes/coins to be paid out for a certain amount. I used real numbers
in my program to make provision for the cents and this is where I
encountered the following problem:

Sometimes the computer rounds off the last cent and thus displays the
wrong amount of cents to be paid out. For example, say the amount to be
paid out requires 2 cents, but the computer rounds the number off and
there is displayed on the screen that 3 cents need to be paid out.

I must round the number to be displayed on the screen off, or otherwise I
get a large decimal number. The following is the code I used:

(starting from the if to check for the 2 cents)

if (remainder>=0.02) and (remainder<0.05) then
writeln('You can pay with ',trunc(remainder/0.02), '2c coins.');

m:=trunc(remainder/0.02);
remainder:=remainder-0.02*m;

(this is the problem part)

if (remainder>=0.01) and (remainder<0.02) then
writeln('You can pay with ',trunc(remainder/0.01), '1 coins.');

Any suggestions in getting past this  problem would be greatly
appreciated. I have also included the entire program as zipped
attachment.

thanks
thiba...@mweb.co.za

## Re:Problem with real numbers - labour.zip [0/1]

Hi

I would like to write a program that takes as input the daily wage of a
labourer, determine the wage owed to him for a week's work, determine the
amount owed to the labourer for overtime work and then subtract any loan
the labourer may owe to the user from the amount owed to him.

I would, however, also like the program to tell the user the exact amount
of notes/coins to be paid out for a certain amount. I used real numbers
in my program to make provision for the cents and this is where I
encountered the following problem:

Sometimes the computer rounds off the last cent and thus displays the
wrong amount of cents to be paid out. For example, say the amount to be
paid out requires 2 cents, but the computer rounds the number off and
there is displayed on the screen that 3 cents need to be paid out.

I must round the number to be displayed on the screen off, or otherwise I
get a large decimal number. The following is the code I used:

(starting from the if to check for the 2 cents)

if (remainder>=0.02) and (remainder<0.05) then
writeln('You can pay with ',trunc(remainder/0.02), '2c coins.');

m:=trunc(remainder/0.02);
remainder:=remainder-0.02*m;

(this is the problem part)

if (remainder>=0.01) and (remainder<0.02) then
writeln('You can pay with ',trunc(remainder/0.01), '1 coins.');

Any suggestions in getting past this  problem would be greatly
appreciated. I have also included the entire program as zipped
attachment.

thanks
thiba...@mweb.co.za

## Re:Problem with real numbers - labour.zip [0/1]

##### Quote
> I would like to write a program that takes as input the daily wage of a
> labourer, determine the wage owed to him for a week's work, determine the
> amount owed to the labourer for overtime work and then subtract any loan
> the labourer may owe to the user from the amount owed to him.
> I would, however, also like the program to tell the user the exact amount
> of notes/coins to be paid out for a certain amount. I used real numbers
> in my program to make provision for the cents and this is where I
> encountered the following problem:
> Sometimes the computer rounds off the last cent and thus displays the
> wrong amount of cents to be paid out. For example, say the amount to be
> paid out requires 2 cents, but the computer rounds the number off and
> there is displayed on the screen that 3 cents need to be paid out.
> I must round the number to be displayed on the screen off, or otherwise I
> get a large decimal number. The following is the code I used:
> (starting from the if to check for the 2 cents)
>  if (remainder>=0.02) and (remainder<0.05) then
>  writeln('You can pay with ',trunc(remainder/0.02), '2c coins.');
>  m:=trunc(remainder/0.02);
>  remainder:=remainder-0.02*m;
>  (this is the problem part)
>  if (remainder>=0.01) and (remainder<0.02) then
>  writeln('You can pay with ',trunc(remainder/0.01), '1 coins.');
> Any suggestions in getting past this  problem would be greatly
> appreciated. I have also included the entire program as zipped
> attachment.

First, don't use Real data types if you're interested in such
precision - Real variables are only an approximation of the value, and
they're never proper for working with important information such as
money.  Second, you should do all this work with Integer or LongInt data
types, scaled up by multiplying by 100, and the formatting the output by
scaling down (div 100 and mod 100) at that point.  "Integer" data is
precise/exact and is the only way to handle financial information...

## Re:Problem with real numbers - labour.zip [0/1]

##### Quote
thiba...@mweb.co.za wrote:

> Hi

> I would like to write a program that takes as input the daily wage of a
> labourer, determine the wage owed to him for a week's work, determine the
> amount owed to the labourer for overtime work and then subtract any loan
> the labourer may owe to the user from the amount owed to him.

> I would, however, also like the program to tell the user the exact amount
> of notes/coins to be paid out for a certain amount. I used real numbers
> in my program to make provision for the cents and this is where I
> encountered the following problem:

> Sometimes the computer rounds off the last cent and thus displays the
> wrong amount of cents to be paid out. For example, say the amount to be
> paid out requires 2 cents, but the computer rounds the number off and
> there is displayed on the screen that 3 cents need to be paid out.

> I must round the number to be displayed on the screen off, or otherwise I
> get a large decimal number. The following is the code I used:

> (starting from the if to check for the 2 cents)

>  if (remainder>=0.02) and (remainder<0.05) then
>  writeln('You can pay with ',trunc(remainder/0.02), '2c coins.');

>  m:=trunc(remainder/0.02);
>  remainder:=remainder-0.02*m;

>  (this is the problem part)

>  if (remainder>=0.01) and (remainder<0.02) then
>  writeln('You can pay with ',trunc(remainder/0.01), '1 coins.');

> Any suggestions in getting past this  problem would be greatly
> appreciated. I have also included the entire program as zipped
> attachment.

> thanks
> thiba...@mweb.co.za

Rounding with trunc is a well know problem. In your application I would
try to use longints. Either througout your program or by conversion.
This gives much better results with money.

LongNum := Round(100.0*RealNum);
LRemainder := LongNum mod 100;

But it is not sure that it works with your numbers. Often the real
calculations run better if you prefer Mult instead of Div.

Also look for FAQs on
http://www.geocities.com/SiliconValley/2926/tp.html

Franz Glaser, Austria
http://members.eunet.at/meg-glaser

## Re:Problem with real numbers - labour.zip [0/1]

In article <35103359.F958...@eunet.at>, "Ing. Franz Glaser" <meg-
gla...@eunet.at> writes

##### Quote
>thiba...@mweb.co.za wrote:>
>Any suggestions in getting past this
>problem would be greatly
>> appreciated.
>Rounding with trunc is a well know problem. In your application I would
>try to use longints.

No - cents are integer. Work in cents and print in Dollars.
Reals (any size) are always bad news where money is concerned.

Regards -

--
Marcus Morris - South Croydon, LONDON, UK (Mar...@ntos.demon.co.uk)
http://www.ntos.demon.co.uk