Recent

Author Topic: Real numbers in Arrays  (Read 2239 times)

squarepenguin

  • Newbie
  • Posts: 3
Real numbers in Arrays
« on: February 02, 2023, 12:46:27 am »
Hi All,

How can I create an array of floats/real numbers with a step of 0.1, e.g.

Range: [0.1..9]
Steps: 0.1

Desired Array would look like: 0.1, 0.2, 0.3......8.8,8.9,9.0

Apologies if it's been said before, I couldn't find it.

Thank you for your time!

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Real numbers in Arrays
« Reply #1 on: February 02, 2023, 01:03:52 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3.  
  4. uses
  5.   SysUtils;
  6.  
  7. type
  8.   TFloats = array of Double;
  9.  
  10. procedure CreateFloats(const AFrom, ATo, ASteps: Double; var AArray: TFloats);
  11. var
  12.   LIndex: Integer;
  13.   LCurrent: Double;
  14. begin
  15.   if (AFrom >= ATo) then
  16.     Exit;
  17.   LCurrent := AFrom;
  18.   LIndex := 0;
  19.   SetLength(AArray, 0);
  20.   while (LCurrent < (ATo - AFrom + ASteps)) do
  21.   begin
  22.     SetLength(AArray, Succ(LIndex));
  23.     AArray[LIndex] := LCurrent;
  24.     LCurrent := LCurrent + ASteps;
  25.     Inc(LIndex);
  26.   end;
  27. end;
  28.  
  29. var
  30.   Arr: TFloats;
  31.   i: Integer;
  32. begin
  33.   CreateFloats(0.1, 9, 0.1, Arr);
  34.   for i := 0 to High(Arr) do
  35.     WriteLn(FloatToStrF(Arr[i], ffFixed, 18, 2));
  36.   {$IFDEF MSWINDOWS}ReadLn;{$ENDIF}
  37. end.

Outputs my image:
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Bogen85

  • Hero Member
  • *****
  • Posts: 612
Re: Real numbers in Arrays
« Reply #2 on: February 02, 2023, 01:09:29 am »
EDIT: Changed how arr_of_real result length was set.

Hi All,

How can I create an array of floats/real numbers with a step of 0.1, e.g.

Range: [0.1..9]
Steps: 0.1

Desired Array would look like: 0.1, 0.2, 0.3......8.8,8.9,9.0

Apologies if it's been said before, I couldn't find it.

Thank you for your time!

KodeZwerg beat me to it...

Mine is a bit different:

Code: Pascal  [Select][+][-]
  1. program array_of_real;
  2. {$mode objfpc}
  3. {$h+}
  4. {$codepage utf8}
  5.  
  6. uses
  7.   sysutils;
  8.  
  9. type
  10.   TArrayOfReal = array of real;
  11.  
  12. function arr_of_real(start, last, step: real): TArrayOfReal;
  13.   var
  14.     current: real;
  15.     i: integer;
  16.   begin
  17.     result := default(TArrayOfReal);
  18.     setLength(result, trunc((last - start)/step)+1);
  19.     current := start;
  20.     for i := low(result) to high(result) do
  21.     begin
  22.       result[i] := current;
  23.       current += step;
  24.     end;
  25.   end;
  26.  
  27. procedure main;
  28.   var
  29.     x: real;
  30.   begin
  31.     for x in arr_of_real(0.1, 9.0, 0.1) do writeln(format('%0.7f', [x]));
  32.   end;
  33.  
  34. begin
  35.   main;
  36. end.

Produces:
Code: Text  [Select][+][-]
  1. $ ./array_of_real
  2. 0.1000000
  3. 0.2000000
  4. 0.3000000
  5. 0.4000000
  6. 0.5000000
  7. 0.6000000
  8. 0.7000000
  9. 0.8000000
  10. 0.9000000
  11. 1.0000000
  12. 1.1000000
  13. 1.2000000
  14. 1.3000000
  15. 1.4000000
  16. 1.5000000
  17. 1.6000000
  18. 1.7000000
  19. 1.8000000
  20. 1.9000000
  21. 2.0000000
  22. 2.1000000
  23. 2.2000000
  24. 2.3000000
  25. 2.4000000
  26. 2.5000000
  27. 2.6000000
  28. 2.7000000
  29. 2.8000000
  30. 2.9000000
  31. 3.0000000
  32. 3.1000000
  33. 3.2000000
  34. 3.3000000
  35. 3.4000000
  36. 3.5000000
  37. 3.6000000
  38. 3.7000000
  39. 3.8000000
  40. 3.9000000
  41. 4.0000000
  42. 4.1000000
  43. 4.2000000
  44. 4.3000000
  45. 4.4000000
  46. 4.5000000
  47. 4.6000000
  48. 4.7000000
  49. 4.8000000
  50. 4.9000000
  51. 5.0000000
  52. 5.1000000
  53. 5.2000000
  54. 5.3000000
  55. 5.4000000
  56. 5.5000000
  57. 5.6000000
  58. 5.7000000
  59. 5.8000000
  60. 5.9000000
  61. 6.0000000
  62. 6.1000000
  63. 6.2000000
  64. 6.3000000
  65. 6.4000000
  66. 6.5000000
  67. 6.6000000
  68. 6.7000000
  69. 6.8000000
  70. 6.9000000
  71. 7.0000000
  72. 7.1000000
  73. 7.2000000
  74. 7.3000000
  75. 7.4000000
  76. 7.5000000
  77. 7.6000000
  78. 7.7000000
  79. 7.8000000
  80. 7.9000000
  81. 8.0000000
  82. 8.1000000
  83. 8.2000000
  84. 8.3000000
  85. 8.4000000
  86. 8.5000000
  87. 8.6000000
  88. 8.7000000
  89. 8.8000000
  90. 8.9000000
  91. 9.0000000
« Last Edit: February 02, 2023, 01:28:53 am by Bogen85 »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Real numbers in Arrays
« Reply #3 on: February 02, 2023, 01:32:49 am »
KodeZwerg beat me to it...
:-[

Mine is a bit different
That's why I love Pascal, there are so many ways to solve a problem, yours got the better memory management, mine written quick and dirty :P
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Bogen85

  • Hero Member
  • *****
  • Posts: 612
Re: Real numbers in Arrays
« Reply #4 on: February 02, 2023, 01:36:14 am »
Code: Pascal  [Select][+][-]
  1.   if (AFrom >= ATo) then
  2.     Exit;

Since AFrom and ATo are both always in the in the result set, the check (which mine is missing) should likely be
Code: Pascal  [Select][+][-]
  1.   if (AFrom > ATo) then
  2.     Exit;

Bogen85

  • Hero Member
  • *****
  • Posts: 612
Re: Real numbers in Arrays
« Reply #5 on: February 02, 2023, 01:49:17 am »
I added the check.
As long as last >= first there will be at least one value in the resulting array.

Code: Pascal  [Select][+][-]
  1. program array_of_real;
  2. {$mode objfpc}
  3. {$h+}
  4. {$codepage utf8}
  5.  
  6. uses
  7.   math, sysutils;
  8.  
  9. type
  10.   TArrayOfReal = array of real;
  11.  
  12. function arr_of_real(const start, last, step: real): TArrayOfReal;
  13.   var
  14.     i: integer;
  15.   begin
  16.     result := default(TArrayOfReal);
  17.     if last < start then exit;
  18.     setLength(result, max(1, trunc((last - start)/step) + 1));
  19.     for i := low(result) to high(result) do result[i] := (i * step) + start;
  20.   end;
  21.  
  22. procedure check_arr_of_real(const last: real);
  23.   var
  24.     x: real;
  25.     i: integer = 1;
  26.   begin
  27.     writeln(format('last: %0.7f', [last]));
  28.     for x in arr_of_real(0.1, last, 0.1) do
  29.     begin
  30.       writeln(format('%2d: %0.7f', [i, x]));
  31.       inc(i);
  32.     end;
  33.   end;
  34.  
  35. procedure main;
  36.   begin
  37.     check_arr_of_real(9.0);
  38.     check_arr_of_real(0.1001);
  39.     check_arr_of_real(0.1);
  40.   end;
  41.  
  42. begin
  43.   main;
  44. end.

Output:
Code: Text  [Select][+][-]
  1. $ ./array_of_real
  2. last: 9.0000000
  3.  1: 0.1000000
  4.  2: 0.2000000
  5.  3: 0.3000000
  6. ...
  7. 88: 8.8000000
  8. 89: 8.9000000
  9. 90: 9.0000000
  10. last: 0.1001000
  11.  1: 0.1000000
  12. last: 0.1000000
  13.  1: 0.1000000
  14.  

« Last Edit: February 02, 2023, 02:11:22 am by Bogen85 »

Thaddy

  • Hero Member
  • *****
  • Posts: 15643
  • Censorship about opinions does not belong here.
Re: Real numbers in Arrays
« Reply #6 on: February 02, 2023, 07:06:27 am »
Simpler way. Note you can not use a real as array index, but you can scale:
Code: Pascal  [Select][+][-]
  1. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  2. {$mode objfpc}
  3. var
  4.    A:Array of real;
  5.    step:real = 0.1;
  6.    first:integer = 0;
  7.    last:integer = 89;
  8.    i:integer;
  9. begin
  10.    Setlength(A, Last-first+1);
  11.    for i:=first to last do A[i] := step + i * step;
  12.    for i:=first to last do writeln(A[i]:8:1);
  13.    readln;
  14. end.
« Last Edit: February 02, 2023, 07:11:55 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

squarepenguin

  • Newbie
  • Posts: 3
Re: Real numbers in Arrays
« Reply #7 on: February 02, 2023, 11:39:35 pm »

Kays

  • Hero Member
  • *****
  • Posts: 604
  • Whasup!?
    • KaiBurghardt.de
Re: Real numbers in Arrays
« Reply #8 on: February 06, 2023, 02:20:34 am »
[…] How can I create an array of floats/real numbers with a step of 0.1, e.g. […] Desired Array would look like: 0.1, 0.2, 0.3......8.8,8.9,9.0 […]
Do you reeeaaally need an array of said contents? Because this is hardly an exercise by itself; you do intend using these values in a subsequent step, right?

I claim the “best” implementation would skip this array population step and integrate the index/10 expression into the first calculation step instead. Maybe in the context of SIMD optimizations you would pre-populate a scale.
Yours Sincerely
Kai Burghardt

squarepenguin

  • Newbie
  • Posts: 3
Re: Real numbers in Arrays
« Reply #9 on: February 08, 2023, 11:56:18 pm »
[…] How can I create an array of floats/real numbers with a step of 0.1, e.g. […] Desired Array would look like: 0.1, 0.2, 0.3......8.8,8.9,9.0 […]
Do you reeeaaally need an array of said contents? Because this is hardly an exercise by itself; you do intend using these values in a subsequent step, right?

I claim the “best” implementation would skip this array population step and integrate the index/10 expression into the first calculation step instead. Maybe in the context of SIMD optimizations you would pre-populate a scale.
----
Hi Kai,

Thank you too.
It was to help build a combobox with those values only (which worked well (I hope)).
The combobox procedure looked liked this in the end (I'm only a beginner!).

begin
       Setlength(A, Last-first+1);
       for i:=first to last do A := step + i * step;
       for count := Low(A) to High(A) do ComboBox.Items.Add(FloatToStr(A[count]));
end;     

Thanks

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: Real numbers in Arrays
« Reply #10 on: February 09, 2023, 12:25:48 am »
squarepenguin, I think Kays is suggesting something like:
Code: Pascal  [Select][+][-]
  1. for i:=first to last do
  2.   ComboBox.Items.Add(FloatToStr(step + i * step));

cheers
S.

PS: BTW, I think there is a typo in your code:
Code: Pascal  [Select][+][-]
  1. for i:=first to last do A[i-first] := step + i * step;
Note that dynamic arrays must start at 0(zero).
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

dseligo

  • Hero Member
  • *****
  • Posts: 1365
Re: Real numbers in Arrays
« Reply #11 on: February 09, 2023, 05:55:44 am »
begin
       Setlength(A, Last-first+1);
       for i:=first to last do A := step + i * step;
       for count := Low(A) to High(A) do ComboBox.Items.Add(FloatToStr(A[count]));
end;     

Use codetags for source. In code above index i is lost because forum software thinks is italic.
Code: Pascal  [Select][+][-]
  1. begin
  2.        Setlength(A, Last-first+1);
  3.        for i:=first to last do A[i] := step + i * step;
  4.        for count := Low(A) to High(A) do ComboBox.Items.Add(FloatToStr(A[count]));
  5. end;

Weiss

  • Full Member
  • ***
  • Posts: 171
Re: Real numbers in Arrays
« Reply #12 on: March 09, 2023, 01:04:26 am »
Hi All,

How can I create an array of floats/real numbers with a step of 0.1, e.g.

Range: [0.1..9]
Steps: 0.1


sounds like you were looking for something very matlab/octave/scilab. Like a= [0.1:0.1:9]. Boom! its done. It is built in, part of language. In any other language you are doing it “manually”.

jamie

  • Hero Member
  • *****
  • Posts: 6556
Re: Real numbers in Arrays
« Reply #13 on: March 11, 2023, 03:18:11 pm »
And here I thought and fractional index into an array was sought.

 In any case,  a Advanced Record can be made with Properties, Methodes and operators to create what looks like any form of an array.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018