Comparing floats

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Comparing floats

Alexandre Bergel
Hi!

In one of my test, I want to check the equality of an array of floats.
The following goes yellow:
        self assert: (service outgoingLinks collect: #bandwidth) asArray = #(0.876846926482685 14.7478812578828).

But this one goes green:
        self assert: (service outgoingLinks collect: #bandwidth) asArray printString =  '#(0.876846926482685 14.7478812578828)' .

Any idea how I can avoid a conversion to strings?
In JUnit, there is assertEquals(double, double, delta). Where delta is an approximation.

Cheers,
Alexandre

--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.






_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

Marcus Denker-4

On Jul 26, 2010, at 10:24 AM, Alexandre Bergel wrote:

> Hi!
>
> In one of my test, I want to check the equality of an array of floats.
> The following goes yellow:
> self assert: (service outgoingLinks collect: #bandwidth) asArray = #(0.876846926482685 14.7478812578828).
>
> But this one goes green:
> self assert: (service outgoingLinks collect: #bandwidth) asArray printString =  '#(0.876846926482685 14.7478812578828)' .
>
> Any idea how I can avoid a conversion to strings?
>


there is a method #closeTo: on Float that compares in a way that should be ok for tests like this.

        Marcus

--
Marcus Denker  -- http://www.marcusdenker.de
INRIA Lille -- Nord Europe. Team RMoD.


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

Alexandre Bergel
Excellent! Thanks!

Cheers,
Alexandre


On 26 Jul 2010, at 10:29, Marcus Denker wrote:

>
> On Jul 26, 2010, at 10:24 AM, Alexandre Bergel wrote:
>
>> Hi!
>>
>> In one of my test, I want to check the equality of an array of floats.
>> The following goes yellow:
>> self assert: (service outgoingLinks collect: #bandwidth) asArray = #(0.876846926482685 14.7478812578828).
>>
>> But this one goes green:
>> self assert: (service outgoingLinks collect: #bandwidth) asArray printString =  '#(0.876846926482685 14.7478812578828)' .
>>
>> Any idea how I can avoid a conversion to strings?
>>
>
>
> there is a method #closeTo: on Float that compares in a way that should be ok for tests like this.
>
> Marcus
>
> --
> Marcus Denker  -- http://www.marcusdenker.de
> INRIA Lille -- Nord Europe. Team RMoD.
>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.






_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project