Author Topic: Random functions as core language functions  (Read 6452 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
Random functions as core language functions
« on: September 18, 2011, 05:35:20 PM »
Charles, I was wondering why you don't have random functions as part of the language core or have a math.inc that comes with oxygen?

In case you ever decide to add them, here is a nice site explaining all the popular functions and shows the algorithms.
http://www.johndcook.com/cpp_TR1_random.html

Additionally here is a list of what users coming from the c world might expect for math as part of the language and constants.
http://www.cs.cf.ac.uk/Dave/C/node17.html

« Last Edit: September 18, 2011, 06:14:42 PM by kryton9 »

Charles Pegge

  • Guest
Re: Random functions as core language functions
« Reply #1 on: September 18, 2011, 07:33:32 PM »
Creating a Maths.inc: Good idea Kent.

At present there is no standard rnd() function. Some are good for speed, others for high statistical quality. Yet others for creating hash tables optimised for ascii.

Here are the speedy ones used in inc/glo2/texture.inc. The noise functions are slightly different in that an index number is passed to get a pseudo-random number. Each index value always returns the same number. This is a requirement of Perlin noise.

Code: OxygenBasic
  1.   '===============
  2.  'NOISE FUNCTIONS
  3.  '===============
  4.  
  5.   Long seed=0x12345678 'initial seed value
  6.  
  7.   '-----------------------
  8.  Function Rnd() as Single
  9.   '=======================
  10.  '
  11.  Static As Single f, d=1/0x7fffffff
  12.   mov eax,seed
  13.   rol eax,7
  14.   imul eax,eax,13
  15.   mov seed,eax
  16.   push eax
  17.   fild dWord [esp]
  18.   add esp,4
  19.   fmul dWord d
  20.   fstp dWord f
  21.   Function=f
  22.   End Function
  23.  
  24.   '---------------------
  25.  Function Rndi() as sys
  26.   '=====================
  27.  '
  28.  mov eax,seed
  29.   xor eax,0x35353535
  30.   imul eax,eax
  31.   ror eax,17
  32.   imul eax,seed
  33.   ror eax,7
  34.   mov seed,eax
  35.   return eax
  36.   End Function
  37.  
  38.  
  39.   '------------------------------
  40.  Function Noise(sys i) as Single
  41.   '==============================
  42.  Static As Single f, d=1/0x7fffffff
  43.   mov eax,i
  44.   xor eax,0x35353535
  45.   imul eax,eax
  46.   ror eax,17
  47.   imul eax,i
  48.   ror eax,7
  49.   push eax
  50.   fild dWord [esp]
  51.   add esp,4
  52.   fmul dWord d
  53.   fstp dWord _return
  54.   End Function
  55.  
  56.  
  57.   '----------------------------
  58.  Function Noiseb(sys i) as sys
  59.   '============================
  60.  Static As Single f, d=1/0x7fffffff
  61.   mov eax,i
  62.   xor eax,0x35353535
  63.   imul eax,eax
  64.   ror eax,17
  65.   imul eax,i
  66.   ror eax,7
  67.   and eax,255
  68.   mov _return,eax
  69.   End Function
  70.  
  71.  

Charles

« Last Edit: September 18, 2011, 08:52:38 PM by Charles Pegge »

kryton9

  • Guest
Re: Random functions as core language functions
« Reply #2 on: September 18, 2011, 07:51:48 PM »
Would the asm code run faster than code written in oxygen? If so, then I think it would be nice if you could assemble a math.inc library when you have some time.

Charles Pegge

  • Guest
Re: Random functions as core language functions
« Reply #3 on: September 18, 2011, 09:08:05 PM »
There are 2 instructions unavailable in Basic: rol and ror. (rotating the bits to the left and to the right). Otherwise it would be possible to produce very similar binary without using any assembler.

Yes a maths source library could also include other useful algorithms like successive approximation, vector and matrix arithmetic and other tricky things that tax the mind.

We have examples for some of these already in the examples/math folder.

Charles
« Last Edit: September 18, 2011, 09:12:31 PM by Charles Pegge »

JRS

  • Guest
Re: Random functions as core language functions
« Reply #4 on: September 19, 2011, 12:24:05 AM »
Quote
Otherwise it would be possible to produce very similar binary without using any assembler.

Sort of like building a log cabin with a hatchet.  :D

Peter

  • Guest
Re: Random functions as core language functions
« Reply #5 on: September 19, 2011, 01:18:18 AM »

 
Quote
Sort of like building a log cabin with a hatchet.

 What then finally collapses.  :D

Peter

  • Guest
Re: Random functions as core language functions
« Reply #6 on: September 19, 2011, 02:10:39 AM »
Hi Kent,

I do something of the kind:
Code: [Select]
double randDouble(double low, double high)
 {
 double temp;

 /* swap low & high around if the user makes no sense */
 if (low > high)
 {
 temp = low;
 low = high;
 high = temp;
 }

 /* calculate the random number & return it */
 temp = (rand() / (static_cast<double>(RAND_MAX) + 1.0))
 * (high - low) + low;
 return temp;
 }

Charles Pegge

  • Guest
Re: Random functions as core language functions
« Reply #7 on: September 19, 2011, 04:00:28 AM »

If speed is a concern then I would avoid using an algorithm that involves division.

Division takes at least 30 clock cycles in contrast to multiplication (~2 cycles) and other arith/logic instructions.

Charles

Peter

  • Guest
Re: Random functions as core language functions
« Reply #8 on: September 19, 2011, 05:34:05 AM »
Hi Charles,

Not quite correct. Take 1000 Birds!   :D
Hold any Key!

Code: [Select]
indexbase 0
include "window.h"
Finit

SetWindow "Birds",800,600,w_1
SetFont 12,12,0,""

While WinExit=0
cls 0
For jx=0 To 1000
SetText "*", Rand(0,1800) /Log(10),Rand(0,1400) /Log(10), RGB(Rand(64,255),Rand(64,255),Rand(64,255))
Next
Pause

DoEvents
SwapBuffers
Wend

WinEnd
« Last Edit: September 19, 2011, 05:37:36 AM by peter »

Charles Pegge

  • Guest
Re: Random functions as core language functions
« Reply #9 on: September 19, 2011, 06:21:58 AM »

Logarithms run to over 100 cpu clocks Peter :)

Any ideas about what a Maths.inc should contain? I suggest a few seminal procedures rather than trying to cover all possibilities.

Charles

Peter

  • Guest
Re: Random functions as core language functions
« Reply #10 on: September 19, 2011, 07:44:42 AM »
yes,  Vector arithmetics!   ::)

Aurel

  • Guest
Re: Random functions as core language functions
« Reply #11 on: September 21, 2011, 09:28:36 AM »
Well i even don't know that there is no RND(x) in oxygen.
I would like to see hash table,linked list also in core

Charles Pegge

  • Guest
Re: Random functions as core language functions
« Reply #12 on: September 21, 2011, 12:42:17 PM »
I'm developing a hash class. It is quite a bulky item (200 lines), so I will keep it separate from the Maths.

You can find it in src/HashStore.o2bas

Here is the hash encoder:

Code: [Select]
 '-------------------------------
  method hashcode(string s) as sys
  '===============================
  xor eax,eax
  mov edx, [s]     ' name pointer
  mov ecx, [edx-4] ' length
  (
  cmp ecx,0
  jnz exit
  return 0 'EMPTY WORD WILL TRIGGER HASHCODE ERROR LATER
  )
  mov ah,  cl      ' assume max length 255
  mov al,  [edx]   ' 1st letter
  cmp cl,1
  (
    jz exit
    shl eax,3
    xor al,  [edx+1] ' 2nd letter
    shl eax,3
    (
      rol eax,1
      xor al, [edx]
      inc edx
      dec ecx
      jnz repeat
    )
  )
  return eax
  '
  end method

Charles
« Last Edit: September 21, 2011, 12:43:55 PM by Charles Pegge »

Aurel

  • Guest
Re: Random functions as core language functions
« Reply #13 on: September 21, 2011, 10:30:50 PM »
Great... :)

Peter

  • Guest
Re: Random functions as core language functions
« Reply #14 on: September 22, 2011, 02:43:18 AM »
Hi Charles,

something  Maths for Maths.inc

Code: [Select]
Function sgn (Single a) as single                 'sign
  single sgn
  iF a < 0 
     sgn= -1
  Else 
     sgn = 1
  End iF
Return sgn
End Function

Function Sec(single x) as single                    'Secant
Return 1 / Cos(x)
End Function

Function CoSec(single x) as single                  'CoSecant         
Return 1 / Sin(x)
End Function

Function CoTan(single x) as single                   'CoTangent 
Return 1 / Tan(x)
End Function

Function ArcSin(single x) as single                  'Inverse Sine
Return Atn(x / Sqr(-x*x+1))
End Function

Function ArcCos(single x) as single                  'Inverse Cosine
Return Atn(-x / Sqr(-x*x+1))+2*Atn(1)
End Function

Function ArcSec(single x) as single
Return Atn(x / Sqr(x*x-1))+Sgn((x)-1)*(2* Atn(1))  'Inverse Secant
End Function

Function HSin(single x) as single                    'Hyperbolic Sine
Return (Exp(x)-Exp(-x)) /2
End Function

Function HCos(single x) as single                    'Hyperbolic Cosine
Return (Exp(x) + Exp(-x)) /2
End Function

Function HTan(single x) as single                    'Hyperbolic Tangent
Return (Exp(x)-Exp(-x)) / (Exp(x)+Exp(-x))
End Function