Board index » delphi » pascal integers and real numbers

pascal integers and real numbers

Hi Dudes,
        I had a statement
        tax_paying := tax_paying - (30000 * 0.02);

Tax_paying as you would imagine was an integer, however, I got a type
mismatch error when I compiled the program with tax_paying as an integer,
(or Longint) if I compiled it when tax_paying was a real, the answer was
not what I expected (something like 6e000+02 came up). Can you help?

See you
Jacky

 

Re:pascal integers and real numbers


In article <66e10g$ff...@tst.hk.super.net>,
Luk Hing Yuen <wh...@is4.hk.super.net> wrote:

Quote
>Hi Dudes,
>    I had a statement
>        tax_paying := tax_paying - (30000 * 0.02);

>Tax_paying as you would imagine was an integer, however, I got a type
>mismatch error when I compiled the program with tax_paying as an integer,
>(or Longint) if I compiled it when tax_paying was a real, the answer was
>not what I expected (something like 6e000+02 came up). Can you help?

How about tax_paying:=tax_paying-(30000 div 50). What was what you
wanted. 6.0000E+02 is 600. Do not use reals to in tax calculations.

You cannot assign floating point values into integer variables just like
that. You need to specify truncation (trunc() or rounding (round()).
Beware that the result of rounding in case of exactly xxxx.5  depends on
whether you use coprocessor {$N+} or not.

Osmo

Re:pascal integers and real numbers


Quote
In article <348a9bf...@lynx.ozramp.net.au>, QarnoS <qar...@mindless.com> wrote:
>wh...@is4.hk.super.net (Luk Hing Yuen) was quite brave when they
>trasmitted the following message:

>>Hi Dudes,
>>        I had a statement
>>        tax_paying := tax_paying - (30000 * 0.02);

>>Tax_paying as you would imagine was an integer, however, I got a type
>>mismatch error when I compiled the program with tax_paying as an integer,
>>(or Longint) if I compiled it when tax_paying was a real, the answer was
>>not what I expected (something like 6e000+02 came up). Can you help?

>try:

>tax_paying:=tax_paying - INT(30000 * 0.02);

Int produces a real value so it does not work.

Quote

>OR

>tax_paying:=tax_paying - ROUND(30000 * 0.02);

>Both will work just fine.

Osmo

Re:pascal integers and real numbers


On 7 Dec 1997 11:24:00 GMT, wh...@is4.hk.super.net (Luk Hing Yuen)
wrote:

Quote
>Hi Dudes,
>    I had a statement
>        tax_paying := tax_paying - (30000 * 0.02);

>Tax_paying as you would imagine was an integer, however, I got a type
>mismatch error when I compiled the program with tax_paying as an integer,
>(or Longint) if I compiled it when tax_paying was a real, the answer was
>not what I expected (something like 6e000+02 came up). Can you help?

>See you
>Jacky

Try this:

leave tax_paying as a real number, and when you write it to the screen
or file, use something like this:

WriteLn (tax_paying:10:2);

This will get rid of the scientific notation.

Kevin

Re:pascal integers and real numbers


Hi Jacky,

The reason for this is that multiplying by a fraction (or, in general,
by a non-integer number) is the same as dividing, which yeilds,
always, a REAL result. If you are sure that you need to get an integer
result in this place, always, then you may use Round(30000*0.02), but
if the 30000 or the 0.02 are a result of something that changes during
program execution, and thus may cause a different (and not always
integer) result, a better bet would be to declare Tax_Paying as Real.

Btw, Round is better here than any other function, because it
compensates for rounding during internel machine calculations.

Good luck,

Eytan.

On 7 Dec 1997 11:24:00 GMT, wh...@is4.hk.super.net (Luk Hing Yuen)
wrote:

Quote
>Hi Dudes,
>    I had a statement
>        tax_paying := tax_paying - (30000 * 0.02);

>Tax_paying as you would imagine was an integer, however, I got a type
>mismatch error when I compiled the program with tax_paying as an integer,
>(or Longint) if I compiled it when tax_paying was a real, the answer was
>not what I expected (something like 6e000+02 came up). Can you help?

>See you
>Jacky

Re:pascal integers and real numbers


wh...@is4.hk.super.net (Luk Hing Yuen) was quite brave when they
trasmitted the following message:

Quote
>Hi Dudes,
>    I had a statement
>        tax_paying := tax_paying - (30000 * 0.02);
>Tax_paying as you would imagine was an integer, however, I got a type
>mismatch error when I compiled the program with tax_paying as an integer,
>(or Longint) if I compiled it when tax_paying was a real, the answer was
>not what I expected (something like 6e000+02 came up). Can you help?

try:

tax_paying:=tax_paying - INT(30000 * 0.02);

OR

tax_paying:=tax_paying - ROUND(30000 * 0.02);

Both will work just fine. What you were doing wrong is, you were
trying to subtract a REAL number (30000 * 0.02) from an INTEGER
(tax_payer) - You cant do this. Either make tax_payer a REAL value or
use one of the above methods.

Re:pascal integers and real numbers


In article <66e10g$ff...@tst.hk.super.net>, wh...@is4.hk.super.net says...

Quote

>Hi Dudes,
>        I had a statement
>        tax_paying := tax_paying - (30000 * 0.02);

>Tax_paying as you would imagine was an integer, however, I got a type
>mismatch error when I compiled the program with tax_paying as an integer,
>(or Longint) if I compiled it when tax_paying was a real, the answer was
>not what I expected (something like 6e000+02 came up). Can you help?

>See you
>Jacky

Dear Jacky,

You need to format the answer.  Tell the program how many decimal places you
want to see in the answer.  Since this is tax I would think 2 places would
suffice.
Bob

Other Threads