# Unit testing with class and floats

**URL:** <https://rubytalk.org/t/unit-testing-with-class-and-floats/48650>\
**Category:** ruby-talk\
**Created:** [27 August 2008 08:51 UTC](https://rubytalk.org/t/unit-testing-with-class-and-floats/48650 "2008-08-27T08:51:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Akhtar](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Adam\_Akhtar](https://rubytalk.org/u/Adam_Akhtar)\
**Post date:** [27 August 2008 08:51 UTC](https://rubytalk.org/t/unit-testing-with-class-and-floats/48650/1 "2008-08-27T08:51:14Z")

</div>

Hi there

im learning unit testing and have just recently started using classes  
(very basic no superclass etc) and now running into problems.

I have a function which returns a class ojbect that contains float  
variables.  
The assert\_equal is constantly failing. However when it prints out a  
summary of the two things i compared in the assert they are exaclty the  
same.

So im wondering if its something to do with classes, perhaps i have to  
define a method in the class which tells assert how to compare objects  
of that class

or its to do with the fact there are floats in the object (i checked  
previous posts and google and found out there are issues with floats and  
assert)

or perhaps both.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Adam\_Akhtar](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Adam\_Akhtar](https://rubytalk.org/u/Adam_Akhtar)\
**Post date:** [27 August 2008 08:55 UTC](https://rubytalk.org/t/unit-testing-with-class-and-floats/48650/2 "2008-08-27T08:55:28Z")

</div>

sorry a (simplified) example

def simpleclass  
&nbsp;&nbsp;&nbsp;attr\_accessor :big\_num #its a float!!!  
end

expected = simpleclass.new(105.234)

assert\_equal(expected, somefunction(blahblah))

i expect somefunction to return a class with 105.234

in the summary it shows that the function does in fact do that but it  
still fails it.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [27 August 2008 09:03 UTC](https://rubytalk.org/t/unit-testing-with-class-and-floats/48650/3 "2008-08-27T09:03:45Z")

</div>

When comparing floats there is a chance that although the floats might  
be represented the same on screen there might be differences in their  
storage. For instance 1.0 and 1.0000001 might look the same on the  
screen but will not be the same if compared with ==.

You should rather use assert\_in\_delta when comparing two floats.

Farrel

> **···**
>
> 2008/8/27 Adam Akhtar \<adamtemporary@gmail.com\>:
> 
> > sorry a (simplified) example
> > 
> > def simpleclass  
> > &nbsp;&nbsp;attr\_accessor :big\_num #its a float!!!  
> > end
> > 
> > expected = simpleclass.new(105.234)
> > 
> > assert\_equal(expected, somefunction(blahblah))
> > 
> > i expect somefunction to return a class with 105.234
> > 
> > in the summary it shows that the function does in fact do that but it  
> > still fails it.
> 
> --  
> Aimred - Ruby Development and Consulting
> 
> > **[Aimred - Home - Ruby and Rails Development in Cape Town, South
> Africa](http://www.aimred.com)**
> >
> > Aimred is a specialist Ruby and Ruby on Rails development house and consultancy based in Cape Town, South Africa.

---

<div class="post-metadata">

**Author:** ![Adam\_Akhtar](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Adam\_Akhtar](https://rubytalk.org/u/Adam_Akhtar)\
**Post date:** [28 August 2008 05:03 UTC](https://rubytalk.org/t/unit-testing-with-class-and-floats/48650/4 "2008-08-28T05:03:02Z")

</div>

ahhh i thought you could only use assert\_in\_delta with only floats  
directly. Ill try passing my class objects with it.

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
