# Abs but for ratios

**URL:** <https://discourse.julialang.org/t/abs-but-for-ratios/7245>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 22, 2017, 12:32pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245 "2017-11-22T12:32:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 22, 2017, 12:32pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245/1 "2017-11-22T12:32:56Z")

</div>

In order to make sure the _difference_ between two numbers is positive, we use `abs` on their subtraction.  
Is there an equivalent function that makes sure the _ratio_ between two numbers is (say) larger than one?

I know I can:

```julia
fun(Φ) = Φ < 1 ? 1/Φ : Φ

```

But the same can be argued for `abs`:

```julia
abs(Δ) = Δ < 0 ? -Δ : Δ

```

I’m just wondering if I missed something.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 22, 2017, 2:13pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245/2 "2017-11-22T14:13:42Z")

</div>

> [@yakir12](#):
>
> Is there an equivalent function that makes sure the ratio between two numbers is (say) larger than one?

I have not encountered a standard name for this. But if the domain is positive, why not use logs and take the absolute difference, eg

```julia
abs(log(r))

```

?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 22, 2017, 2:24pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245/3 "2017-11-22T14:24:41Z")

</div>

Yea, you mean `exp(abs(log(r)))`, expensive though. OK, thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 22, 2017, 3:04pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245/4 "2017-11-22T15:04:37Z")

</div>

No, I would just work with logarithms. This is pretty standard in statistics, as probabilities/likelihoods multiplied or divided run out of the range of `Float64` very quickly (usually a few hundred or at best thousand values), whereas `log`s take you very far, and you just add/subtract.

If the ratios are trivial, you would of course be OK with something like

```julia
ifelse(r < 1, 1/r, r)

```

so it all depends on your application.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 22, 2017, 3:07pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245/5 "2017-11-22T15:07:46Z")

</div>

Nice! Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/julialang/original/3X/1/2/12829a7ba92b924d4ce81099cbf99785bee9b405.png) [@system](https://discourse.julialang.org/u/system)\
**Post date:** [November 23, 2017, 3:07pm UTC](https://discourse.julialang.org/t/abs-but-for-ratios/7245/6 "2017-11-23T15:07:47Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
