Forum > Beginners

Real numbers in Arrays

(1/3) > >>

squarepenguin:
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:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{\$IFDEF MSWINDOWS}{\$APPTYPE CONSOLE}{\$ENDIF} uses  SysUtils; type  TFloats = array of Double; procedure CreateFloats(const AFrom, ATo, ASteps: Double; var AArray: TFloats);var  LIndex: Integer;  LCurrent: Double;begin  if (AFrom >= ATo) then    Exit;  LCurrent := AFrom;  LIndex := 0;  SetLength(AArray, 0);  while (LCurrent < (ATo - AFrom + ASteps)) do  begin    SetLength(AArray, Succ(LIndex));    AArray[LIndex] := LCurrent;    LCurrent := LCurrent + ASteps;    Inc(LIndex);  end;end; var  Arr: TFloats;  i: Integer;begin  CreateFloats(0.1, 9, 0.1, Arr);  for i := 0 to High(Arr) do    WriteLn(FloatToStrF(Arr[i], ffFixed, 18, 2));  {\$IFDEF MSWINDOWS}ReadLn;{\$ENDIF}end.
Outputs my image:

Bogen85:
EDIT: Changed how arr_of_real result length was set.

--- Quote from: squarepenguin 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!

--- End quote ---

KodeZwerg beat me to it...

Mine is a bit different:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program array_of_real;{\$mode objfpc}{\$h+}{\$codepage utf8} uses  sysutils; type  TArrayOfReal = array of real; function arr_of_real(start, last, step: real): TArrayOfReal;  var    current: real;    i: integer;  begin    result := default(TArrayOfReal);    setLength(result, trunc((last - start)/step)+1);    current := start;    for i := low(result) to high(result) do    begin      result[i] := current;      current += step;    end;  end; procedure main;  var    x: real;  begin    for x in arr_of_real(0.1, 9.0, 0.1) do writeln(format('%0.7f', [x]));  end; begin  main;end.
Produces:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---\$ ./array_of_real0.10000000.20000000.30000000.40000000.50000000.60000000.70000000.80000000.90000001.00000001.10000001.20000001.30000001.40000001.50000001.60000001.70000001.80000001.90000002.00000002.10000002.20000002.30000002.40000002.50000002.60000002.70000002.80000002.90000003.00000003.10000003.20000003.30000003.40000003.50000003.60000003.70000003.80000003.90000004.00000004.10000004.20000004.30000004.40000004.50000004.60000004.70000004.80000004.90000005.00000005.10000005.20000005.30000005.40000005.50000005.60000005.70000005.80000005.90000006.00000006.10000006.20000006.30000006.40000006.50000006.60000006.70000006.80000006.90000007.00000007.10000007.20000007.30000007.40000007.50000007.60000007.70000007.80000007.90000008.00000008.10000008.20000008.30000008.40000008.50000008.60000008.70000008.80000008.90000009.0000000

KodeZwerg:

--- Quote from: Bogen85 on February 02, 2023, 01:09:29 am ---KodeZwerg beat me to it...
--- End quote ---
:-[

--- Quote from: Bogen85 on February 02, 2023, 01:09:29 am ---Mine is a bit different
--- End quote ---
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

Bogen85:

--- Quote from: KodeZwerg on February 02, 2023, 01:03:52 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if (AFrom >= ATo) then    Exit;
--- End quote ---

Since AFrom and ATo are both always in the in the result set, the check (which mine is missing) should likely be
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if (AFrom > ATo) then    Exit;