# "Not a rational" constant?

**URL:** <https://discourse.julialang.org/t/not-a-rational-constant/104039>\
**Category:** General Usage\
**Tags:** rational\
**Created:** [September 19, 2023, 6:39pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039 "2023-09-19T18:39:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [September 19, 2023, 6:39pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039/1 "2023-09-19T18:39:55Z")

</div>

In porting some C code I came across a value called NaR, which was defined as Not a Rational (when returned due to error from a function returning a rational). In terms of type consistency, is there a rational in Julia that would be equivalent to NaR?

I suppose 1//0 could be one, and this is what I am now using, but this becomes Inf when converted to float, and I’d prefer it convert to NaN, so that

```
(NaR isa Rational) == true 
float(NaR) == NaN

```

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [September 19, 2023, 6:53pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039/2 "2023-09-19T18:53:14Z")

</div>

`Rational{T}` is a leaf type for each `T` so you can’t define your own elements in the type.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 19, 2023, 7:05pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039/3 "2023-09-19T19:05:22Z")

</div>

> [@wherrera](#):
>
> this becomes Inf when converted to float, and I’d prefer it convert to NaN

There’s no problem with making your own function. Something along the lines of

```julia
function finitefloat(x)
	f = float(x)
	return isfinite(f) ? f : oftype(f,NaN)
end

```

Note: changing how rationals interact with general functions _without_ going through wrappers like this example is not something easy to do without the extremely dangerous and **strongly** discouraged act of [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) (and even then, is only “easy” if it doesn’t accidentally break a bunch of other stuff you’re trying to use).

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 19, 2023, 7:07pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039/4 "2023-09-19T19:07:54Z")

</div>

Theoretically NaN _could_ map to `0//0`, but that’s explicitly not allowed:

```julia
julia> 0//0
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)

```

Instead, I’d probably return `missing` in situations like this and explicitly `coalesce` to `NaN` upon converting to float.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 19, 2023, 7:21pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039/5 "2023-09-19T19:21:27Z")

</div>

What is the reasoning here? Seems rather inconsistent to me.  
Also I find the errors to be inconsistent:

```julia
0//0 # ArgumentError of constructor, explains zero/zero is invalid Rational
(1//0)*0 # DivideError in gcd, says division by 0 not allowed

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 19, 2023, 7:23pm UTC](https://discourse.julialang.org/t/not-a-rational-constant/104039/6 "2023-09-19T19:23:00Z")

</div>

The reasoning is basically just that allowing `1//0` and `-1//0` as infinite values works out well in the math without adding extra checks while supporting `0//0` adds a special case to basically any function that deals with rationals.
