1

R6RS 3.4 Implementation requirements reads

... Potentially inexact operations such as sqrt, when applied to exact arguments, should produce exact answers whenever possible (for example the square root of an exact 4 ought to be an exact 2).

(1) Does this mean that (* 0 2.2) must produce 0.0 but never 0 as result?

However, this is not required. ...

(2) Does this mean that (/ 4 2) may also produce 2.0 as result?

(3) And are there implementations that provide integer roots in this manner? How do I get the functionality of integer square roots in general? (Here is the index) (Please no recursive/iterative implementation)

false
  • 10,264
  • 13
  • 101
  • 209
  • 1
    In Racket, `(exact? (sqrt 4))`, `(exact? (* 0 2.2))` and `(exact? (/ 4 2))` produce `#t`. So does `(exact? (/ 4 3))` since `1 1/3` is an exact number. – uselpa Dec 30 '14 at 12:19
  • @LePetitPrince: With freshly installed Racket v6.1.1, `(* (/ 0.0 0.0) 0)` and `(* 0 (/ 1 0.0))` and `(/ 0 0.0)` all produce `0` (thus exact). Is this OK? – false Dec 30 '14 at 13:33
  • I would have expected `(/ 0.0 0.0)` to throw an exception but it yields `+nan.0` because `(inexact? 0.0)` is `#t`. The rest is explained [here](http://docs.racket-lang.org/reference/numbers.html). – uselpa Dec 30 '14 at 15:57
  • FWIW, Guile yields different results for some expressions: `(exact? (* 0 2.2))` is `#f`, `(* (/ 0.0 0.0) 0)`, `(* 0 (/ 1 0.0))` and `(/ 0 0.0)` are `+nan.0`, which seems more logical to me. – uselpa Dec 30 '14 at 16:01
  • @LePetitPrince `(/ 0 0.0)` _should_ be NaN. But _multiplying_ anything by an exact zero is, mathematically speaking, an exact zero, so Scheme implementations are allowed (but not required) to account for that. – C. K. Young Dec 30 '14 at 16:02
  • 1
    @ChrisJester-Young Multiplying a number by exact 0 is 0. Multiplying a NaN by 0 should remain NaN, because the NaN does not magically become a number. – uselpa Dec 30 '14 at 16:05
  • @LePetitPrince True, I think I can agree with that, in the sense that NaN means "I don't know what this number is", and that number could potentially be infinity. But certainly, any non-infinite number multiplied by exact 0 is 0. – C. K. Young Dec 30 '14 at 16:10
  • @ChrisJester-Young: That's what I try to understand. If you have a float value, that value is by definition inexact. It may come from some inexact computation. Actually, maybe it really should be NaN, but due to roundoffs, the division `(/ zero zero)` did not yield that result. – false Dec 30 '14 at 16:32

1 Answers1

2

R6RS has exact-integer-sqrt. It returns two values, the square root and the remainder. You can discard the second value if you want.

Implementations are allowed (but not required) to consider (* 0 x) (multiplying anything by an exact zero) to be exact zero for any x. See the R6RS description of the * procedure.

false
  • 10,264
  • 13
  • 101
  • 209
C. K. Young
  • 219,335
  • 46
  • 382
  • 435