# Unit testing with class and floats

**URL:** <https://www.ruby-forum.com/t/unit-testing-with-class-and-floats/146989>\
**Category:** Ruby\
**Created:** [August 27, 2008, 10:55am UTC](https://www.ruby-forum.com/t/unit-testing-with-class-and-floats/146989 "2008-08-27T10:55:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 27, 2008, 10:55am UTC](https://www.ruby-forum.com/t/unit-testing-with-class-and-floats/146989/1 "2008-08-27T10:55:47Z")

</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.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 27, 2008, 10:59am UTC](https://www.ruby-forum.com/t/unit-testing-with-class-and-floats/146989/2 "2008-08-27T10:59:57Z")

</div>

sorry a (simplified) example

def simpleclass  
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.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 27, 2008, 11:09am UTC](https://www.ruby-forum.com/t/unit-testing-with-class-and-floats/146989/3 "2008-08-27T11:09:12Z")

</div>

2008/8/27 Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.

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

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 28, 2008, 7:07am UTC](https://www.ruby-forum.com/t/unit-testing-with-class-and-floats/146989/4 "2008-08-28T07:07:41Z")

</div>

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

Thanks
