# Floats and Integers

In Nyquist, as with List, there are two types of numbers – Integers and Floating Point numbers.

An integer is a whole number with no decimal part, such as 1, 2, 99, 1000000.

A floating point number is referred to as a FLONUM, or simply a “float” (floating point number) and is most easily defined as a number with a decimal separator, for example 3.142 or 0. or 3.2
Notice that a FLONUM does not need to have a decimal fractional value. 3.000 is the same as 3. which is the same as 3.0 but is not the same a 3 (note that the final “3” does not have a decimal point – it is an integer.

To set the variable “A” to the integer value “3”

[cc lang=”lisp”](setq A 3)[/cc]
To set the value “B” to the floating point  value “3”

[cc lang=”lisp”](setq B 3.0)[/cc] 3.0 is a FLONUM – it has a decimal point (decimal separator)

Using the conditional construct “case” we can test the variables A and B to see if they are FLONUMs or Integers.
[cc lang=”lisp”](case (type-of a)
(flonum   (print “A is a float”))
(fixnum   (print “A is an integer”)))

(case (type-of b)
(flonum   (print “B is a float”))
(fixnum   (print “B is an integer”)))[/cc]

[cc lang=”lisp”](if (= a b)(print “A = B”)(print “A and B Not Equal”))[/cc]

### How to convert an integer into a floating point number.

This is just a simple matter of using the function “float

[cc lang=”lisp”](setq A (float A))[/cc]

### How to convert a floating point number into an integer.

To convert a FLONUM into an integer, we simply “truncate” the number.
In this example, we will test to see “if” b is a floating point number, and if it is then we will truncate it.

[cc lang=”lisp”](if (= B (round B))(setq B (truncate B)))[/cc]
We can then test them again as before:

[cc lang=”lisp”](case (type-of b)
(flonum   (print “B is a float”))
(fixnum   (print “B is an integer”)))

….[/cc]

Putting it all together – you can run this in the Nyquist Prompt effect, but be sure to press the “Debug” button to see the output.

[cc lang=”lisp”]
(setq A 3)
(setq B 3.0)

(case (type-of a)
(flonum   (print “A is a float”))
(fixnum   (print “A is an integer”)))

(case (type-of b)
(flonum   (print “B is a float”))
(fixnum   (print “B is an integer”)))

(if (= a b)(print “A = B”)(print “A and B Not Equal”))

(setq A (float A))

(if (= B (round B))(setq B (truncate B)))

(case (type-of a)
(flonum   (print “A is a float”))
(fixnum   (print “A is an integer”)))

(case (type-of b)
(flonum   (print “B is a float”))
(fixnum   (print “B is an integer”)))
[/cc]

This site uses Akismet to reduce spam. Learn how your comment data is processed.