Recent

Author Topic: Convert double to Real and File access issue  (Read 377 times)

kepler

  • Newbie
  • Posts: 3
Convert double to Real and File access issue
« on: August 15, 2019, 05:18:23 pm »
Good afternoon,

First of all, my congratulations to the team that developed Lazurus. It's a great software, easy, intuitive, but very advanced at the same time and very versatile.

My topic has to do with several pre routines I made, and that I'm trying to gather in one simple program. Pascal is not my first Programming language unfortunately.

My first problem is that I have a function that returns an array of values with double precision, which I need to use as REAL numbers. I cannot seem to make the conversion from one to another (at least with a correct syntax, or without having completely wrong results - like an expected result of 0.0022111 giving 16777.99.... for instance).

Another issue is a file that I open, and a function that uses it. I want to open it only once and access when it is needed.
How can I check if the file was allready opened once and share the data in Global terms in the whole program (that is, in a global scope)?

Can someone help me out here? I would apreciate it very much.

Clear skies

Kepler




Thaddy

  • Hero Member
  • *****
  • Posts: 9276
Re: Convert double to Real and File access issue
« Reply #1 on: August 15, 2019, 05:42:55 pm »
First of all do you mean Pascal REAL? in that case use the unit real48utils from the rtl-extra package that comes with FPC. A Pascal REAL is 48 bits and is a legacy - deprecated - format, now aliased to a double.
Or do you mean REAL as used in other languages? ?  In that case it depends on what they gave defined: it can be a single (4 bytes), double (8 bytes) or extended, which is either 8 or 10 bytes
depending on platform.

Note that if you really mean the Pascal type, use the utility functions just for import and export of data and work with doubles internally.
In the other case you have to determine the size of the real from e.g. the C code. That would probably be either single or double (or erroneously, but often size_t) and is defined somewhere early in the headers.
If you know C(++) you will see that immediately.

If you know the source of your data and its programming language I can give you more advice, but these are the usual suspects
« Last Edit: August 15, 2019, 06:05:50 pm by Thaddy »
also related to equus asinus.

BeanzMaster

  • Full Member
  • ***
  • Posts: 182
Re: Convert double to Real and File access issue
« Reply #2 on: August 15, 2019, 06:08:51 pm »
First of all do you mean Pascal REAL? in that case use the unit real48 that comes with FPC. A Pascal REAL is 48 bits and is a legacy format.
Or do you mean REAL as used in other languages? ?  In that case it depends on what they gave defined: it can be a single (4 bytes), double (8 bytes) or extended, which is either 8 or 10 bytes depending on platform.


Hi, Real48 unit doesn't exit. Real48 is defined in mathh.inc in system unit  ;) The only operation you can do with is :

 
Code: Pascal  [Select]
  1.   function Real2Double(r : real48) : double; and  operator := (b:real48) d:double;  

So if you want precision and you want run your application in multiple os choose Double else Single. because if you choose Extended, is 10 bytes on Linux64 and 8 bytes on Win64 so it can be a problem. see https://wiki.freepascal.org/IEEE_754_formats and https://wiki.freepascal.org/Data_type fore more details

Thaddy

  • Hero Member
  • *****
  • Posts: 9276
Re: Convert double to Real and File access issue
« Reply #3 on: August 15, 2019, 06:25:21 pm »
It is real48utils, I corrected that. Our posts crossed . You posted at exactly the same time, didn't you get a warning? I did not so I was first.

Apart from that: your answer is wrong in the sense that it is incomplete: if the dataset is in REAL's you need the full set of operations to store it back in REAL's.
You may even need to use the overloaded operators to keep the same precision as real48, otherwise your data becomes unverifiable at the other side.
(But I don't think these are Pascal reals, but foreign reals.)

I am awaiting the answer by OP.
« Last Edit: August 15, 2019, 06:32:50 pm by Thaddy »
also related to equus asinus.

jamie

  • Hero Member
  • *****
  • Posts: 2140
Re: Convert double to Real and File access issue
« Reply #4 on: August 15, 2019, 07:02:38 pm »
The file format is needed, is it text or binary based ?
Number 1 at blue screen app creations!

kepler

  • Newbie
  • Posts: 3
Re: Convert double to Real and File access issue
« Reply #5 on: August 15, 2019, 07:46:56 pm »
Hi,

I'm sorry for the late reply - I did not activate instant email notification.

Your explanations are very useful and complete. But I'm still learning. My strong is VB and C/C++ in aaps for the PC (along with Perl, PHP, JS, etc. for web).

The REAL is defined on Lazurus, and so the double - no C/C++ involved (or no DLL). I'm working on Windows 7, 64 bits.

The file I'm accessing is binary. It seems that the units are all able to access it. I'm trying to reset the array to zeros (maintaining the same properties), but I'm doing it with a for loop. Is there a more simple way to do it? I tryed some google solutions, without luck.

I am programming in PASCAL - fresh units on Lazarus.

Clear skies

Kepler


Thaddy

  • Hero Member
  • *****
  • Posts: 9276
Re: Convert double to Real and File access issue
« Reply #6 on: August 15, 2019, 07:53:25 pm »
Real on Lazarus is an alias for double for years now. Problem solved.
(The Lazarus people should add deprecated to the real alias. It should always be declared as double.)
Code: Pascal  [Select]
  1. type
  2.   real = type double deprecated 'use double instead';
  3.  

Although at some points in the rtl real aliases are used in implementation section  includes. Which is a valid option as well. Still there's no native support for real real's apart from the real48utils unit.
« Last Edit: August 15, 2019, 08:06:02 pm by Thaddy »
also related to equus asinus.

Thaddy

  • Hero Member
  • *****
  • Posts: 9276
Re: Convert double to Real and File access issue
« Reply #7 on: August 15, 2019, 08:18:27 pm »
Is there a more simple way to do it?
Yes, use the default() intrinsic.

Example:
Code: Pascal  [Select]
  1. {$mode delphi}
  2. type
  3.   TSomeArrayOfDouble = array[0..99] of double;
  4.  
  5. var
  6.   a:TSomeArrayOfDouble;
  7.   d:Double;
  8. begin
  9.   a :=Default(TSomeArrayOfDouble);
  10.   for d in a do write(d:1:1,' ');
  11. end.

A fixed length array will be initialized to all zero's. A dynamic array will be initialized to empty.
More importantly this works also inside procedures or functions.

And Default() works on all types. Syntax is: <var> := Default(<type>);

Simplest example:
Code: Pascal  [Select]
  1. {$mode delphi}
  2. var
  3.   i:integer = 100;
  4.   s:string = 'I will disappear!' ;
  5. begin
  6.   writeln(i);
  7.   writeln(s);
  8.   i:=Default(integer);
  9.   s:=Default(string);
  10.   writeln(i,' should be zero');
  11.   writeln(s,' should be empty');;
  12. end.
« Last Edit: August 15, 2019, 08:35:10 pm by Thaddy »
also related to equus asinus.

kepler

  • Newbie
  • Posts: 3
Re: Convert double to Real and File access issue
« Reply #8 on: August 15, 2019, 10:13:34 pm »
Good evening

Thank you very much Thaddy. Problem(s) solved  :)

Clear skies,

Kepler