racketfixnumtyped-racket

What is the Fixnum data type in Racket?


I'd like to make my racket program typed/racket to speed it up. My program does operations on matrices using the Matrix data type. I was suggested to use the data type Fixnum.

I have some matrices like,
(: X (Matrix Fixnum)) (define X (matrix [[0 1] [2 3]] : Fixnum))
which is OK.

However, if the numbers in the matrix have decimals, I get an error.
(: Y (Matrix Fixnum)) (define Y (matrix [[0 0.5] [1.5 2.5]] : Fixnum))

Type Checker: type mismatch
expected: Fixnum
given: Positive-Flonum in: 0.5

Fixnum is a machine type (I don't fully understand what this is; not sure if it's relavant). I know fixnum is limited to 64 bits. But why can't 0.5 (or any decimal, it seems) be a Fixnum?


Solution

  • Please read the documentation (http://docs.racket-lang.org/reference/numbers.html). Here's an excerpt:

    A fixnum is an exact integer whose two’s complement representation fit into 31 bits on a 32-bit platform or 63 bits on a 64-bit platform

    So, no, fixnum is an integer.

    Note that 0.5 and 1/2 are different in Racket. 0.5 is a Flonum, while 1/2 is an Exact-Rational.


    By the way, I see no point that you have to mention about "Matrix". Merely the following code causes a type error.

    #lang typed/racket
    
    (: X Fixnum)
    (define X 0.5)
    

    It's the best when you try to understand a program to trim the program down as much as you could so that you could understand what's going on easily.