Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 632
sherry Avatar asked Sep 27 '11 05:09

sherry


1 Answers

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

like image 52
Lukas Renggli Avatar answered Nov 04 '22 07:11

Lukas Renggli