preserves/implementations/racket/preserves/preserves/float-bytes.rkt

25 lines
760 B
Racket

#lang racket/base
;; Conversion between binary32 and binary64 big-endian external format (byte-vectors) and
;; internal double-precision floating-point numbers, with special attention paid to
;; preservation of the quiet/signaling bit of NaNs, which otherwise is frequently disturbed by
;; hardware-level conversion between single and double precision.
(provide bytes->float
float->bytes
bytes->double
double->bytes)
(require "float.rkt")
(define (bytes->float bs)
(float (floating-point-bytes->real bs #t 0 4)))
(define (float->bytes v)
(real->floating-point-bytes (float-value v) 4 #t))
(define (bytes->double bs)
(floating-point-bytes->real bs #t 0 8))
(define (double->bytes v)
(real->floating-point-bytes v 8 #t))