Forum > Beginners
Real numbers in Arrays
Bogen85:
I added the check.
As long as last >= first there will be at least one value in the resulting array.
--- 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 math, sysutils; type TArrayOfReal = array of real; function arr_of_real(const start, last, step: real): TArrayOfReal; var i: integer; begin result := default(TArrayOfReal); if last < start then exit; setLength(result, max(1, trunc((last - start)/step) + 1)); for i := low(result) to high(result) do result[i] := (i * step) + start; end; procedure check_arr_of_real(const last: real); var x: real; i: integer = 1; begin writeln(format('last: %0.7f', [last])); for x in arr_of_real(0.1, last, 0.1) do begin writeln(format('%2d: %0.7f', [i, x])); inc(i); end; end; procedure main; begin check_arr_of_real(9.0); check_arr_of_real(0.1001); check_arr_of_real(0.1); end; begin main;end.
Output:
--- 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_reallast: 9.0000000 1: 0.1000000 2: 0.2000000 3: 0.3000000...88: 8.800000089: 8.900000090: 9.0000000last: 0.1001000 1: 0.1000000last: 0.1000000 1: 0.1000000
Thaddy:
Simpler way. Note you can not use a real as array index, but you can scale:
--- 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";}};} ---{$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}{$mode objfpc}var A:Array of real; step:real = 0.1; first:integer = 0; last:integer = 89; i:integer;begin Setlength(A, Last-first+1); for i:=first to last do A[i] := step + i * step; for i:=first to last do writeln(A[i]:8:1); readln;end.
squarepenguin:
@Thaddy, Bogen85, KodeZwerg - thank you kindly for the help and time on this one, much appreciated!
--- Quote from: KodeZwerg on February 02, 2023, 01:32:49 am ---
--- End quote ---
--- Quote from: Bogen85 on February 02, 2023, 01:49:17 am ---
--- End quote ---
--- Quote from: Thaddy on February 02, 2023, 07:06:27 am ---
--- End quote ---
Kays:
--- Quote from: squarepenguin on February 02, 2023, 12:46:27 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 […]
--- End quote ---
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.
squarepenguin:
--- Quote from: Kays on February 06, 2023, 02:20:34 am ---
--- Quote from: squarepenguin on February 02, 2023, 12:46:27 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 […]
--- End quote ---
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.
--- End quote ---
----
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
Navigation
[0] Message Index
[#] Next page
[*] Previous page