REScala - Strange behaviour

Asfaloth
Windoof-User
Windoof-User
Beiträge: 27
Registriert: 7. Nov 2012 10:36

REScala - Strange behaviour

Beitrag von Asfaloth »

Hi,

i'm playing with REScala a bit. Maybe someone can explain the behaviour of this.

First the Code:

Code: Alles auswählen

object ReactiveTest extends App {
  val e = new ImperativeEvent[Double]
  val f = new ImperativeEvent[Int]
  
  val eWindow = e.last(5)
  val fWindow = f.last(5)
  
  println(eWindow().length)
  println(fWindow().length)
  
  val eMean = Signal {
    eWindow().sum / eWindow().length
  }
  
  val fMean = Signal {
    fWindow().sum / fWindow().length // Line 22
  }
}
Notice everything is identical exept the Event type (one is Int, the other one is Double).

Now this is the output:

Code: Alles auswählen

0
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at ReactiveTest$$anonfun$2.apply(ReactiveStuff.scala:22)
	at ReactiveTest$$anonfun$2.apply(ReactiveStuff.scala:21)
	at react.SignalSynt.reEvaluate(DynamicDepReactives.scala:93)
	at react.SignalSynt.<init>(DynamicDepReactives.scala:77)
	at react.SignalSynt$.apply(DynamicDepReactives.scala:133)
	at react.SignalSynt$.apply(DynamicDepReactives.scala:137)
	at ReactiveTest$delayedInit$body.apply(ReactiveStuff.scala:21)
	at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
	at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
	at scala.App$$anonfun$main$1.apply(App.scala:71)
	at scala.App$$anonfun$main$1.apply(App.scala:71)
	at scala.collection.immutable.List.foreach(List.scala:318)
	at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
	at scala.App$class.main(App.scala:71)
	at ReactiveTest$.main(ReactiveStuff.scala:7)
	at ReactiveTest.main(ReactiveStuff.scala)
Why does the mean work for Double Events, but crashes for Int Events?!?!?!?

What works: Double, Float
What crashes: Int, Short

Thanks for your help!
Asfaloth

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: REScala - Strange behaviour

Beitrag von imaier »

There really is a difference between floating point division and integer division (e.g., since they follow/approximate different mathematical laws). Most languages unfortunately do not distinguish between the two and overload the same / operator.

In Java, from which Scala inherits its arithmetic expression behavior, the result of floating point division by zero is infinity. For integer division it will throw an exception as you observed:

http://docs.oracle.com/javase/specs/jls ... ls-15.17.2

(The rationale in the difference in this particular point is most likely that integers simply don't reserve a representation for infinity. They use their whole N bits for numbers between -2^(n-1) and 2^n. Floating point numbers, however, include infinity (different NaNs, negative zero etc). You probably heard that in another course or can read it here http://en.wikipedia.org/wiki/IEEE_754-1985)

Update: Just to say it explicitly, the behavior you observe has nothing to do with REScala.

Cheers,
Ingo

Asfaloth
Windoof-User
Windoof-User
Beiträge: 27
Registriert: 7. Nov 2012 10:36

Re: REScala - Strange behaviour

Beitrag von Asfaloth »

Thanks for your reply.

A devision by zero where one operand is a float indeed returns NaN (like the IEEE 754 specification defines).

Code: Alles auswählen

  val t = 0.0f / 0
  val s = 0 / 0.0f
  println("t=" + t)
  println("s=" + s)
Output

Code: Alles auswählen

t=NaN
s=NaN
I didn't know this behaviour, thanks for pointing it out! :-)

Antworten

Zurück zu „Archiv“