Comparing floats

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

Comparing floats

stepharo
Hi

I'm writing a simple converter between Celcius and Farhenheit as an
example to start programming.


Converter >> convertFarhenheit: anInteger
     ^ ((anInteger - 32) / 1.8)

ConverterTest >> testFToC

     | converter |
     converter := TemperatureConverter new.
     self assert: ((converter convertFarhenheit: 86) = 30.0).
     self assert: ((converter convertFarhenheit: 50) = 10).
     self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)


My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)

I do not want to have such ugly test.

I tried either to control the output

Converter >> convertFarhenheit: anInteger
     ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1

Not a good idea

Or to compare with closeTo:

     self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)

does not work because we cannot set the precision.

So at ESUG I briefly discuss that with Nicolas Cellier and I need help.

Should I introduce in pharo closeTo:interval:

Any suggestions that a newby can understand is welcome.


Stef



Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

Esteban A. Maringolo
Why not use "equalsTo:"?

Floats are tricky, you either round your result before comparing or
change the comparison method.

0.333333333333333333 equalsTo: (1/3) "true"

Regards!
Esteban A. Maringolo


2016-09-02 15:41 GMT-03:00 stepharo <[hidden email]>:

> Hi
>
> I'm writing a simple converter between Celcius and Farhenheit as an example
> to start programming.
>
>
> Converter >> convertFarhenheit: anInteger
>     ^ ((anInteger - 32) / 1.8)
>
> ConverterTest >> testFToC
>
>     | converter |
>     converter := TemperatureConverter new.
>     self assert: ((converter convertFarhenheit: 86) = 30.0).
>     self assert: ((converter convertFarhenheit: 50) = 10).
>     self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)
>
>
> My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)
>
> I do not want to have such ugly test.
>
> I tried either to control the output
>
> Converter >> convertFarhenheit: anInteger
>     ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1
>
> Not a good idea
>
> Or to compare with closeTo:
>
>     self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)
>
> does not work because we cannot set the precision.
>
> So at ESUG I briefly discuss that with Nicolas Cellier and I need help.
>
> Should I introduce in pharo closeTo:interval:
>
> Any suggestions that a newby can understand is welcome.
>
>
> Stef
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

Henrik Nergaard
In reply to this post by stepharo
You could implement a custom assert in ConverterTest  to do the comparison, something like

assert: aNumber withPrecision: precision equals: otherNumber

        self
        assert: (aNumber round: precision)
        equals: otherNumber

assert: aNumber closeTo: otherNumber

        assert: aNumber withPrecision: self defaultPrecision equals: otherNumber

defaultPrecision
        ^ 2


Best regards,
Henrik

-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of stepharo
Sent: Friday, September 2, 2016 8:41 PM
To: Any question about pharo is welcome <[hidden email]>
Subject: [Pharo-users] Comparing floats

Hi

I'm writing a simple converter between Celcius and Farhenheit as an example to start programming.


Converter >> convertFarhenheit: anInteger
     ^ ((anInteger - 32) / 1.8)

ConverterTest >> testFToC

     | converter |
     converter := TemperatureConverter new.
     self assert: ((converter convertFarhenheit: 86) = 30.0).
     self assert: ((converter convertFarhenheit: 50) = 10).
     self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)


My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)

I do not want to have such ugly test.

I tried either to control the output

Converter >> convertFarhenheit: anInteger
     ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1

Not a good idea

Or to compare with closeTo:

     self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)

does not work because we cannot set the precision.

So at ESUG I briefly discuss that with Nicolas Cellier and I need help.

Should I introduce in pharo closeTo:interval:

Any suggestions that a newby can understand is welcome.


Stef



Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

Uko2
how about having assert:closeTo: ?

> On 02 Sep 2016, at 21:09, Henrik Nergaard <[hidden email]> wrote:
>
> You could implement a custom assert in ConverterTest  to do the comparison, something like
>
> assert: aNumber withPrecision: precision equals: otherNumber
>
> self
> assert: (aNumber round: precision)
> equals: otherNumber
>
> assert: aNumber closeTo: otherNumber
>
> assert: aNumber withPrecision: self defaultPrecision equals: otherNumber
>
> defaultPrecision
> ^ 2
>
>
> Best regards,
> Henrik
>
> -----Original Message-----
> From: Pharo-users [mailto:[hidden email]] On Behalf Of stepharo
> Sent: Friday, September 2, 2016 8:41 PM
> To: Any question about pharo is welcome <[hidden email]>
> Subject: [Pharo-users] Comparing floats
>
> Hi
>
> I'm writing a simple converter between Celcius and Farhenheit as an example to start programming.
>
>
> Converter >> convertFarhenheit: anInteger
>     ^ ((anInteger - 32) / 1.8)
>
> ConverterTest >> testFToC
>
>     | converter |
>     converter := TemperatureConverter new.
>     self assert: ((converter convertFarhenheit: 86) = 30.0).
>     self assert: ((converter convertFarhenheit: 50) = 10).
>     self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)
>
>
> My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)
>
> I do not want to have such ugly test.
>
> I tried either to control the output
>
> Converter >> convertFarhenheit: anInteger
>     ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1
>
> Not a good idea
>
> Or to compare with closeTo:
>
>     self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)
>
> does not work because we cannot set the precision.
>
> So at ESUG I briefly discuss that with Nicolas Cellier and I need help.
>
> Should I introduce in pharo closeTo:interval:
>
> Any suggestions that a newby can understand is welcome.
>
>
> Stef
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

stepharo
would be nice to extend Sunit with that


Le 3/9/16 à 00:21, Yuriy Tymchuk a écrit :

> how about having assert:closeTo: ?
>
>> On 02 Sep 2016, at 21:09, Henrik Nergaard <[hidden email]> wrote:
>>
>> You could implement a custom assert in ConverterTest  to do the comparison, something like
>>
>> assert: aNumber withPrecision: precision equals: otherNumber
>>
>> self
>> assert: (aNumber round: precision)
>> equals: otherNumber
>>
>> assert: aNumber closeTo: otherNumber
>>
>> assert: aNumber withPrecision: self defaultPrecision equals: otherNumber
>>
>> defaultPrecision
>> ^ 2
>>
>>
>> Best regards,
>> Henrik
>>
>> -----Original Message-----
>> From: Pharo-users [mailto:[hidden email]] On Behalf Of stepharo
>> Sent: Friday, September 2, 2016 8:41 PM
>> To: Any question about pharo is welcome <[hidden email]>
>> Subject: [Pharo-users] Comparing floats
>>
>> Hi
>>
>> I'm writing a simple converter between Celcius and Farhenheit as an example to start programming.
>>
>>
>> Converter >> convertFarhenheit: anInteger
>>      ^ ((anInteger - 32) / 1.8)
>>
>> ConverterTest >> testFToC
>>
>>      | converter |
>>      converter := TemperatureConverter new.
>>      self assert: ((converter convertFarhenheit: 86) = 30.0).
>>      self assert: ((converter convertFarhenheit: 50) = 10).
>>      self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)
>>
>>
>> My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)
>>
>> I do not want to have such ugly test.
>>
>> I tried either to control the output
>>
>> Converter >> convertFarhenheit: anInteger
>>      ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1
>>
>> Not a good idea
>>
>> Or to compare with closeTo:
>>
>>      self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)
>>
>> does not work because we cannot set the precision.
>>
>> So at ESUG I briefly discuss that with Nicolas Cellier and I need help.
>>
>> Should I introduce in pharo closeTo:interval:
>>
>> Any suggestions that a newby can understand is welcome.
>>
>>
>> Stef
>>
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

stepharo
In reply to this post by Henrik Nergaard
Thanks I will try that.

Stef


Le 2/9/16 à 21:09, Henrik Nergaard a écrit :

> You could implement a custom assert in ConverterTest  to do the comparison, something like
>
> assert: aNumber withPrecision: precision equals: otherNumber
>
> self
> assert: (aNumber round: precision)
> equals: otherNumber
>
> assert: aNumber closeTo: otherNumber
>
> assert: aNumber withPrecision: self defaultPrecision equals: otherNumber
>
> defaultPrecision
> ^ 2
>
>
> Best regards,
> Henrik
>
> -----Original Message-----
> From: Pharo-users [mailto:[hidden email]] On Behalf Of stepharo
> Sent: Friday, September 2, 2016 8:41 PM
> To: Any question about pharo is welcome <[hidden email]>
> Subject: [Pharo-users] Comparing floats
>
> Hi
>
> I'm writing a simple converter between Celcius and Farhenheit as an example to start programming.
>
>
> Converter >> convertFarhenheit: anInteger
>       ^ ((anInteger - 32) / 1.8)
>
> ConverterTest >> testFToC
>
>       | converter |
>       converter := TemperatureConverter new.
>       self assert: ((converter convertFarhenheit: 86) = 30.0).
>       self assert: ((converter convertFarhenheit: 50) = 10).
>       self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)
>
>
> My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)
>
> I do not want to have such ugly test.
>
> I tried either to control the output
>
> Converter >> convertFarhenheit: anInteger
>       ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1
>
> Not a good idea
>
> Or to compare with closeTo:
>
>       self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)
>
> does not work because we cannot set the precision.
>
> So at ESUG I briefly discuss that with Nicolas Cellier and I need help.
>
> Should I introduce in pharo closeTo:interval:
>
> Any suggestions that a newby can understand is welcome.
>
>
> Stef
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Comparing floats

abergel
In reply to this post by Uko2
JUnit has the equivalent of: assert:equals:range:
It could then be:

self assert: 11.11 equals: 11.11 range: 0.0001

Alexandre


> On Sep 2, 2016, at 7:21 PM, Yuriy Tymchuk <[hidden email]> wrote:
>
> how about having assert:closeTo: ?
>
>> On 02 Sep 2016, at 21:09, Henrik Nergaard <[hidden email]> wrote:
>>
>> You could implement a custom assert in ConverterTest  to do the comparison, something like
>>
>> assert: aNumber withPrecision: precision equals: otherNumber
>>
>> self
>> assert: (aNumber round: precision)
>> equals: otherNumber
>>
>> assert: aNumber closeTo: otherNumber
>>
>> assert: aNumber withPrecision: self defaultPrecision equals: otherNumber
>>
>> defaultPrecision
>> ^ 2
>>
>>
>> Best regards,
>> Henrik
>>
>> -----Original Message-----
>> From: Pharo-users [mailto:[hidden email]] On Behalf Of stepharo
>> Sent: Friday, September 2, 2016 8:41 PM
>> To: Any question about pharo is welcome <[hidden email]>
>> Subject: [Pharo-users] Comparing floats
>>
>> Hi
>>
>> I'm writing a simple converter between Celcius and Farhenheit as an example to start programming.
>>
>>
>> Converter >> convertFarhenheit: anInteger
>>    ^ ((anInteger - 32) / 1.8)
>>
>> ConverterTest >> testFToC
>>
>>    | converter |
>>    converter := TemperatureConverter new.
>>    self assert: ((converter convertFarhenheit: 86) = 30.0).
>>    self assert: ((converter convertFarhenheit: 50) = 10).
>>    self assert: ((converter convertFarhenheit: 52) = 11.11111111111111)
>>
>>
>> My problem is with ((converter convertFarhenheit: 52) = 11.11111111111111)
>>
>> I do not want to have such ugly test.
>>
>> I tried either to control the output
>>
>> Converter >> convertFarhenheit: anInteger
>>    ^ ((anInteger - 32) / 1.8) roundDownTo: 0.1
>>
>> Not a good idea
>>
>> Or to compare with closeTo:
>>
>>    self assert: ((converter convertFarhenheit: 52) closeTo: 11.11)
>>
>> does not work because we cannot set the precision.
>>
>> So at ESUG I briefly discuss that with Nicolas Cellier and I need help.
>>
>> Should I introduce in pharo closeTo:interval:
>>
>> Any suggestions that a newby can understand is welcome.
>>
>>
>> Stef
>>
>>
>>
>
>

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