Skip to content

floatsize

Returns the size of Csound's internal floating-point type in bytes.

Syntax

iBytes = floatsize()
iBytes floatsize

Initialization

floatsize takes no inputs and returns an i-rate value. The result is sizeof(MYFLT), where MYFLT is the type Csound uses for ordinary numeric variables and audio samples.

Result Csound build
4 Single precision, using 32-bit floats
8 Double precision, using 64-bit doubles

The build chooses this type. It stays the same throughout the performance. A 64-bit computer can run either build. Audio-file bit depth is independent of this value.

Examples

Use floatsize when a numeric check needs to allow for different rounding errors in float and double builds. This example squares the square root of two, then checks how close the result is to two. It uses floatsize.csd.

Choose a tolerance for a numeric check
<CsoundSynthesizer>
<CsOptions>
-n -d -m0
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  iBytes = floatsize()
  printf_i "Internal numbers use %g bytes (%g bits)\n", 1, iBytes, 8 * iBytes

  ; Allow more rounding error when Csound uses single precision.
  if iBytes == 4 then
    iTolerance = 0.000001
  else
    iTolerance = 0.000000000001
  endif

  iRoot = sqrt(2)
  iError = abs(iRoot * iRoot - 2)

  ; Compare with a tolerance instead of requiring exact equality.
  if iError < iTolerance then
    prints "Square-root check passed\n"
  else
    prints "Square-root check failed\n"
  endif
endin
</CsInstruments>
<CsScore>
i 1 0 0.01
e
</CsScore>
</CsoundSynthesizer>

The example prints the internal number size and whether the check passed. Its tolerances suit this calculation. Choose tolerances for your own calculations based on the size of the values and the errors you can accept.

See also

Floating-point precision, Miscellaneous opcodes

Credits

Opcode by Victor Lazzarini, December 2024.

New in Csound 7.