# MiniTest::Assertions#assert\_in\_delta $B$N5sF0(B

**URL:** <https://www.ruby-forum.com/t/minitest-assertions-assert-in-delta-b-n5sf0-b/190897>\
**Category:** Development\
**Created:** [June 30, 2010, 11:19am UTC](https://www.ruby-forum.com/t/minitest-assertions-assert-in-delta-b-n5sf0-b/190897 "2010-06-30T11:19:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@okkez](https://www.ruby-forum.com/u/okkez)\
**Post date:** [June 30, 2010, 11:19am UTC](https://www.ruby-forum.com/t/minitest-assertions-assert-in-delta-b-n5sf0-b/190897/1 "2010-06-30T11:19:01Z")

</div>

okkez e$B$G$9!#e(B

e$B%P%0$+$I$&$+\<+?.$,;}$F$J$$$N$G3NG’$5$;$F$/$@$5$$!#e(B

MiniTest::Assertions#assert\_in\_delta  
e$B$N%3!\<%I$O0J2\<$N$h$&$K$J$C$F$$$^$9!#e(B

# Ruby1.9.2

* * *

```
##
# For comparing Floats. Fails unless +exp+ and +act+ are within 

```

+delta+  
# of each other.  
#  
# assert\_in\_delta Math::PI, (22.0 / 7.0), 0.01

```
def assert_in_delta exp, act, delta = 0.001, msg = nil
  n = (exp - act).abs
  msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to be < 

```

#{delta}" }  
assert delta \>= n, msg  
end

```
##
# For comparing Floats. Fails unless +exp+ and +act+ have a 

```

relative  
# error less than +epsilon+.

```
def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
  assert_in_delta a, b, [a, b].min * epsilon, msg
end

```

* * *

assert\_in\_epsilon e$B$Oe(B assert\_in\_delta  
e$B$r8F$S=P$7$F$$$k$N$G$^$H$a$F?t\<0$C$]$/e(B  
e$B=q$-2\<$9$H8!::$7$F$$$kFbMF$O0J2\<$N$h$&$K$J$k$H;W$$$^$9!#e(B

epsilon \> 0 e$B$H$9$k!#e(B  
epsilon \>= |a - b|/min(a, b) — (x)

(1) a \> 0, b \> 0, a \> b  
(x) e$B$N1&JU$O@5e(B

(2) a \< 0, b \< 0, a \> b  
(x) e$B$N1&JU$OIie(B -\> e$B\>o$K%F%9%H$K%Q%9$7$F$7$^$&!)e(B

(3) a \> 0, b \< 0  
(x) e$B$N1&JU$OIie(B -\> e$B\>o$K%F%9%H$K%Q%9$7$F$7$^$&!)e(B

(4) a \< 0, b \> 0  
(x) e$B$N1&JU$OIie(B -\> e$B\>o$K%F%9%H$K%Q%9$7$F$7$^$&!)e(B

e$B$3$N;M$D$N\>l9g$,$"$k$H;W$&$N$G$9$,!"$3$l$O%P%0$G$7$g$&$+!)e(B  
e$B$=$l$H$b!“AjBP8m:9$r7W;;$9$k\>l9g$O!“4|BTCM!”\<BB,CM$H$b$K@5$NCM$Ge(B  
e$B$”$kI,MW$,$"$k$N$G$7$g$&$+!#e(B

---

<div class="post-metadata">

**Author:** ![okkez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@okkez](https://www.ruby-forum.com/u/okkez)\
**Post date:** [June 30, 2010, 11:21am UTC](https://www.ruby-forum.com/t/minitest-assertions-assert-in-delta-b-n5sf0-b/190897/2 "2010-06-30T11:21:21Z")

</div>

2010e$BG/e(B6e$B7ne(B30e$BF|e(B18:17 okkez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> okkez e$B$G$9!#e(B
> 
> e$B%P%0$+$I$&$+\<+?.$,;}$F$J$$$N$G3NG’$5$;$F$/$@$5$$!#e(B
> 
> MiniTest::Assertions#assert\_in\_delta e$B$N%3!\<%I$O0J2\<$N$h$&$K$J$C$F$$$^$9!#e(B

e$B$9$\_$^$;$s!#5$$K$J$C$F$$$k$N$Oe(B assrt\_in\_delta e$B$G$O$J$/e(B  
assert\_in\_epsilon e$B$G$9!#e(B
