Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Foundations: shared types, utilities and file formats

Underneath the numerical chapters are the pieces every program needs: one error type, numeric traits and constants that all crates share, small utilities, and ways to get data in and out. This chapter covers:

  • lib_jsl_core: the error type, numeric traits, array aliases and named constants;
  • lib_jsl_utils: floating-point spacing, unit conversion and terminal plots;
  • lib_jsl_file_io: readers and writers for WAV audio, PNG, JPEG and Netpbm images, CSV tables and NumPy arrays.

The central questions

  • Will other programs read it? A file format is only useful if other software agrees about it. Every format here is tested in both directions against the reference implementations: Pillow (libpng, libjpeg), Python’s wave, csv and zlib modules, SciPy and NumPy.
  • Lossless or lossy? WAV, PNG, Netpbm, CSV and .npy keep the data exactly; JPEG trades exactness for size, guided by a model of what the eye notices.
  • What if the file is damaged or hostile? Decoders return errors rather than panicking, and check the sizes a header claims before allocating for them.

Testing against Python’s libraries

The references come from book/generator/data/make_foundations_refs.py. It writes small files with Pillow 12.2, NumPy 2.4 and the Python standard library, embeds them in the reference file for the library to read, and reads back files written by the library. Constants are checked against mpmath and SciPy’s CODATA 2022 values. The results are on the audio, image and data-file pages.

In the library

You needUseEntry
The workspace’s error type, numeric traits and constants; ULPs, unit conversion, terminal plotsErrorsJSL, constants, ulp, unit_conversion, render_terminal_plotShared types, constants and utilities
How the API is organised (errors, options, results, stability), and moving code from 1.x to 2.0…Options, …Result, nfev, convergedAPI conventions and the 2.0 migration guide
WAV filesread_wav_file, write_wav_file, parse_wav, encode_wavAudio files
PNG, JPEG or Netpbm images; zlib streamsread_png_file, jpeg_encode, ppm_decode, zlib_compressImage files
Tables and arrays to share with spreadsheets and Pythonread_csv_file, read_npy_file, write_npy_fileData files