Board index » delphi » VAL() - Real numbers

VAL() - Real numbers

Hello all, my name is Baydon

  I use TPW v1.5 and am having a problem with the VAL command.
I have a program which retrieves a series of real numbers from a file
The numbers are stored as text from the file, however when I try to use
the procedure Val(S; var V; var Code: Integer); it adds humbers on after
the decimal or even changes the decimal place numbers.

        eg)  '1.01' returns 1.0100000000263534
             '8.75' returns 8.7499873758492485

None of these numbers contain the same number of decimal points so I can't use a real
number extention (ie  x:0:2).

If any one can offer any assistance please e-mail me at cr...@lis.ab.ca

Also just as a suggestion, you might want to add what program you are using for programming
so that others can be more specific when replying.

 

Re:VAL() - Real numbers


The Rowe Family <cr...@lis.ab.ca> writes (Date: Mon, 08 Jul 1996 11:05:22
-0700):
< I use TPW v1.5 and am having a problem with the VAL

Quote
> command.  I have a program which retrieves a series
> of real numbers from a file.  The numbers are stored
> as text from the file, however when I try to use the
> procedure Val(S; var V; var Code: Integer); it adds
> humbers on after the decimal or even changes the
> decimal place numbers.

> eg)  '1.01' returns 1.0100000000263534
>      '8.75' returns 8.7499873758492485

     What is happening here is the destruction of a myth: "Computers don't
make errors."
     T8he numbers you have chosen can't be represented exactly in binary.
For the precision that you have chosen,   1.0100000000263534 is the best
"1.01" can be represented in binary (floating points are binary), and
8.7499873758492485 is the best "8.75" can be represented in binary.
     You can reduce the error by going to greater precision numbers, such
as EXTENDED, but you can't eliminate the error completely after TP 3.0
{without developing your own math routines).  To completely eliminate the
error, you can use Turbo Pascal Version 3, Binary Coded Decimal (BDC)
version.  This stores numbers in base 10 and eliminates this type of
error.  That makes it particularly valuable for the financial programs it
was designed for.  Another language, if available on the P.C., is ZBasic
which allows you to have up to 64 (I think) digit numbers stored
internally in BCD.
     This is a subject that is taught in a good college level programming
course, but is hard to find elsewhere.  Look for a book on numerical
analysis by computers and wade your way through it.

Re:VAL() - Real numbers


In article <31E14DE2.6...@lis.ab.ca>, The Rowe Family  <cr...@lis.ab.ca> wrote:

Quote
>Hello all, my name is Baydon

>  I use TPW v1.5 and am having a problem with the VAL command.
>I have a program which retrieves a series of real numbers from a file
>The numbers are stored as text from the file, however when I try to use
>the procedure Val(S; var V; var Code: Integer); it adds humbers on after
>the decimal or even changes the decimal place numbers.

>    eg)  '1.01' returns 1.0100000000263534
>         '8.75' returns 8.7499873758492485

Is that some kind of joke. I tried with TP 7.0 on Real, single, double
and extended and did not get so bad results (the second one) on any of
them. What type were you using? Did you just make those figures up?

Osmo

Re:VAL() - Real numbers


Hi, this is Baydon.

   And yes I did make those numbers up, however these ones I did not make up

        '1.01'   ----->      1.01000000000022E+0000
        '8.75'   ----->      My mistake, this one didn't change
        '9.23'   ----->      9.22999999999593E+0000
        '52.46'  ----->      5.24600000000210E+0001
        '145.34' ----->      1.45340000000084E+0002

USING TURBO PASCAL FOR WINDOWS VERSION 1.5
..........................................

program Hello;

uses wincrt, strings;

var
  R: Real;
  E: integer;
  S: array[0..255] of Char;
begin
  val('1.01', R, E);
  Str(R, S);
  WriteLn(S);
end.

Re:VAL() - Real numbers


I darkly remember having seen BCD units for TP 4.0 and later. Try SWAG or
a Web-Crawler, if you need them. For financial application, use the
COMP data type and express your numbers in Pence, Penny, Pfennig or
what ever they are using in your place.

On the other hand: Are the numbers in your application so precise, that
you want to print them at 17 significant places?

Remember the old saying: Nothing reveals lack of mathematical
understanding faster than needless accuracy in calculating numbers!

Re:VAL() - Real numbers


In article <31E496A8.5...@lis.ab.ca>, cr...@lis.ab.ca says...

Quote

>Hi, this is Baydon.

>   And yes I did make those numbers up, however these ones I did not
make up

>        '1.01'   ----->      1.01000000000022E+0000
>        '8.75'   ----->      My mistake, this one didn't change
>        '9.23'   ----->      9.22999999999593E+0000
>        '52.46'  ----->      5.24600000000210E+0001
>        '145.34' ----->      1.45340000000084E+0002

>USING TURBO PASCAL FOR WINDOWS VERSION 1.5
>..........................................

>program Hello;

>uses wincrt, strings;

>var
>  R: Real;
>  E: integer;
>  S: array[0..255] of Char;
>begin
>  val('1.01', R, E);
>  Str(R, S);

why not Str(R:6:2,S)  if you only want two decimal places?

- Show quoted text -

Quote

>  WriteLn(S);
>end.

Re:VAL() - Real numbers


In article <31E496A8.5...@lis.ab.ca>, The Rowe Family  <cr...@lis.ab.ca> wrote:

Quote
>Hi, this is Baydon.

>   And yes I did make those numbers up, however these ones I did not make up

Be careful when you make things up.

Quote
>    '1.01'   ----->      1.01000000000022E+0000
>    '8.75'   ----->      My mistake, this one didn't change
>    '9.23'   ----->      9.22999999999593E+0000
>    '52.46'  ----->      5.24600000000210E+0001
>    '145.34' ----->      1.45340000000084E+0002

So? Those look like as they should be.

Quote
>USING TURBO PASCAL FOR WINDOWS VERSION 1.5
>..........................................

>program Hello;

>uses wincrt, strings;

>var
>  R: Real;
>  E: integer;
>  S: array[0..255] of Char;
>begin
>  val('1.01', R, E);
>  Str(R, S);
>  WriteLn(S);
>end.

What you need to do is to limit the accuracy while you output the
figures. The TP 7.0 does that automatically.

Osmo

Other Threads