Jac
Jac

Reputation: 101

Remainder with no integer number in scheme

I need to know the remainder of a division:

(remainder (/ 3 2) 2)

But as (/ 3 2) is not integer gives me an error.

modulo: contract violation expected: integer? given: 1.5 argument position: 1st

How could I solve it?

Upvotes: 2

Views: 4771

Answers (3)

soegaard
soegaard

Reputation: 31147

To determine whether a number is even or not, use even?.

An integer is even if the remainder is zero, when the number is divided by 2.

A non-integer is not even.

(define (my-even? x)
  (if (integer? x)
      (= (remainder x 2) 0)
      #f)))

The builtin functions is called even?.

> (even? 5)
#f

Upvotes: 2

John Clements
John Clements

Reputation: 17203

Aren't you just looking for

(remainder 3 2)

?

Upvotes: 4

Leroy
Leroy

Reputation: 247

If you want to get back a float, one of the operands needs to be a float, e.g. (/ 3.0 2) => 1.5.

If you can't change the numbers inside the division, you can use exact->inexact.

(exact->inexact (/ 3 2)) => 1.5


If you're trying to get the remainder, have you thought about the modulo operator?

Modulo is basically the number 'left over', or the overflow.

(modulo 3 2)
=> 1
(modulo 3 1)
=> 0

Upvotes: 2

Related Questions