A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://zxbasic.readthedocs.io/en/docs/library/fsin.bas/ below:

FSin.bas - Boriel BASIC

This function implements fast Fixed point sin, cos and tan functions. The ones that uses Float type (calling the Sinclair ROM) are much slower as FP Calculations are expensive. This variant is less precise but more suitable for games, for example.

fSin is the basis for the alternatives, since COS(x) can be calculated from SIN(x) and TAN(x) as SIN(x) / COS(x).

The functions should be accurate to about 0.25%, and significantly faster. If you need a lot of trig in your code, and it doesn't need to be pinpoint accuracy, these are good alternatives. Note that they more or less acknowledge they are less accurate by returning values of type Fixed instead of type Float. It should be fine for the actual accuracy returned, and Fixed numbers process faster and smaller than Float ones.

FUNCTION fSin(num as FIXED) as FIXED
  DIM quad as byte
  DIM est1,dif as uByte

  'This change made now that MOD works with FIXED types.
  'This is much faster than the repeated subtraction method for large angles (much > 360)
  'while having some tiny rounding errors that should not significantly affect our results.
  'Note that the result may be positive or negative still, and for SIN(360) might come out
  'fractionally above 360 (which would cause issued) so the below code still is required.

  IF num >= 360 THEN
    num = num MOD 360
  ELSEIF num < 0 THEN
    num = 360 - ABS(num) MOD 360
  END IF

  IF num > 180 then
    quad = -1
    num = num - 180
  ELSE
    quad = 1
  END IF

  IF num > 90 THEN num = 180 - num

  num = num / 2
  dif = num : REM Cast to byte loses decimal
  num = num - dif : REM so this is just the decimal bit

  est1 = PEEK(@sinetable + dif)
  dif = PEEK(@sinetable + dif + 1) - est1 : REM this is just the difference to the next up number.
  num = est1 + (num * dif) : REM base + interpolate to the next value.

  Return (num / 255) * quad

  sinetable:
  Asm
    DEFB 000,009,018,027,035,044,053,062
    DEFB 070,079,087,096,104,112,120,127
    DEFB 135,143,150,157,164,171,177,183
    DEFB 190,195,201,206,211,216,221,225
    DEFB 229,233,236,240,243,245,247,249
    DEFB 251,253,254,254,255,255
  End Asm
END FUNCTION

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4