# Analogue of zero(T) for infinitiy

**URL:** https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027
**Category:** General Usage
**Tags:** question
**Created:** [November 12, 2017, 5:40pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027 "2017-11-12T17:40:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 12, 2017, 5:40pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/1 "2017-11-12T17:40:56Z")

</div>

How do I get the infinity corresponding to a type? Is there an analog of `zero(T)`? Something like:

```julia
inf(Float16)

```

Here is how I would define it myself:

```julia
function inf(::Type{T})::T where T
    one(T) / zero(T)
end

```

Is there a better way?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 12, 2017, 7:18pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/3 "2017-11-12T19:18:32Z")

</div>

> [@e3c6](#):
>
> How do I get the infinity corresponding to a type?

`T(Inf)`. For example, `Float16(Inf)`.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 12, 2017, 8:10pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/4 "2017-11-12T20:10:54Z")

</div>

If we can do `T(0)`, what’s the point of `zero(T)`?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 12, 2017, 8:22pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/5 "2017-11-12T20:22:11Z")

</div>

For any subtype of `Number`, `zero(T)` is just a shorthand for `convert(T, 0)`. See: [https://github.com/JuliaLang/julia/blob/6a23e234e6cc5b4361b5f88614a9ed423dc2c12a/base/number.jl#L238](https://github.com/JuliaLang/julia/blob/6a23e234e6cc5b4361b5f88614a9ed423dc2c12a/base/number.jl#L238)

And for _any_ type T, if you haven’t defined a constructor `T(x)`, then it will also fall back to `convert(T, x)`. See: [https://github.com/JuliaLang/julia/blob/6a23e234e6cc5b4361b5f88614a9ed423dc2c12a/base/sysimg.jl#L114](https://github.com/JuliaLang/julia/blob/6a23e234e6cc5b4361b5f88614a9ed423dc2c12a/base/sysimg.jl#L114)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 12, 2017, 8:24pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/6 "2017-11-12T20:24:56Z")

</div>

So `zero(T)` is strictly equivalent to `T(0)`. Then why do we need `zero(...)`?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 12, 2017, 8:26pm UTC](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/7 "2017-11-12T20:26:02Z")

</div>

> [@e3c6](#):
>
> If we can do `T(0)`, what’s the point of `zero(T)`?

Because `zero` works for types that are not numbers. e.g. matrices. Any type supporting `+` should also define a `zero` function to return the additive identity.

In contrast, `Inf` is pretty much only meaningful for numeric types, specifically floating-point types.
