Search code examples
floating-pointassertsmalltalkvisualworks

assert fails when it should not, in Smalltalk Unit testcase


I'm stumped. Here is my testcase.

theTestArray := #(1.2 3 5.1 7).
self assert: theTestArray  squareOfAllElements = #(1.44 9 26.01 49).

The assert should not fail. On calculating the square of each element is correct. So I did "step into test", shows that the result of the method squareOfAllElements and #(1.44 9 26.01 49) are both the same but assert evaluates to false. why? What am I doing wrong here? Any help is appreciated.


Solution

  • You are dealing with floating point numbers here. Floating point numbers are inexact by definition and you should never compare them using #=.

    For details check Section 1.1 of the draft chapter on floating point numbers of Pharo by Example: http://stephane.ducasse.free.fr/Web/Draft/Float.pdf