Problem with SORTLIB

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Problem with SORTLIB

Post by zeynel1 »

The following code gives "number too big" error.
What am I doing wrong?
I'm trying to sort the array Dem with 29 items in it. Why would that be a big number?

Code: Select all

      REM compute uncertainty in Cavendish density values
      REM cavendish-error.bbc
      INSTALL @lib$+"fnusing"
      INSTALL @lib$+"SORTLIB"

      *SPOOLON output.txt
      DIM Den(28)
      Den()=5.50,\
      \5.61,\
      \4.88,\
      \5.07,\
      \5.26,\
      \5.55,\
      \5.36,\
      \5.29,\
      \5.58,\
      \5.65,\
      \5.57,\
      \5.53,\
      \5.62,\
      \5.29,\
      \5.44,\
      \5.34,\
      \5.79,\
      \5.10,\
      \5.27,\
      \5.39,\
      \5.42,\
      \5.47,\
      \5.63,\
      \5.34,\
      \5.46,\
      \5.30,\
      \5.75,\
      \5.68,\
      \5.85

      sort%=FN_sortinit(0,0)

      C%=DIM(Den(),DIM(Den()))+1
      CALL sort%, Den(0)

      PRINT "Den(28)=";Den(28)

      Mean=SUM(Den())/29
      PRINT "Dim()/29=";FNusing("##.##", Mean)

zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: Problem with SORTLIB

Post by zeynel1 »

I just noticed that this is supposed to be integers, I'm trying to sort floating point numbers. Is that the reason? How do I sort floating point numbers?
Hated Moron

Re: Problem with SORTLIB

Post by Hated Moron »

zeynel1 wrote: Wed 08 Jun 2022, 14:15 The following code gives "number too big" error.
What am I doing wrong?
Most likely it's because you have used sort% rather than sort%% (there should be two percent signs).

This is what it says in the Help manual here:

"Before any sorting operations can be carried out the library must be initialised as follows:"

Code: Select all

sort%% = FN_sortinit(dir%,smode%)
I'm wondering if perhaps you were looking at the Help manual for BBC BASIC for Windows (in which case sort% will work) rather than the Help manual for BBC BASIC for SDL 2.0 which needs sort%%.
zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: Problem with SORTLIB

Post by zeynel1 »

Great thanks! Now it works.

Yes I was looking at the Windows manual.

So this was not an integer issue then. When I saw "%" in sort%, I thought that meant integer.
Hated Moron

Re: Problem with SORTLIB

Post by Hated Moron »

zeynel1 wrote: Wed 08 Jun 2022, 15:40 When I saw "%" in sort%, I thought that meant integer.
That's right, sort% is a 32-bit integer, sort%% is a 64-bit integer. BBC BASIC for Windows is a 32-bit program, BBC BASIC for SDL 2.0 is (or can be - for example on a Mac) a 64-bit program.

The sortlib library can sort any of the BBC BASIC data types (8-bit, 32-bit & 64-bit integers, 64-bit & 80-bit floats, and strings). Indeed you can mix them and have (for example) an integer array as the primary sort key, a float array as a secondary sort key and a string array as a tertiary sort key. It's very flexible.
zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: Problem with SORTLIB

Post by zeynel1 »

Thanks. One more question: I noticed it sorts the original array. Is there a way to keep the original array and create a new sorted array?
Hated Moron

Re: Problem with SORTLIB

Post by Hated Moron »

zeynel1 wrote: Wed 08 Jun 2022, 16:46 I noticed it sorts the original array. Is there a way to keep the original array and create a new sorted array?
Copy the array and sort the copy: :D

Code: Select all

      DIM sorted(DIM(Den(),1))
      sorted() = Den()
      CALL sort%%, sorted(0)