# Promote on ArbFloat issue?

**URL:** https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839
**Category:** Numerics
**Tags:** question
**Created:** [August 15, 2023, 7:10pm UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839 "2023-08-15T19:10:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)
#### Post date: [August 15, 2023, 7:10pm UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839/1 "2023-08-15T19:10:38Z")

</div>

I am trying to make sure that all of my `ArbFloat`s are with the same precision.  
I do not understand why promote works like that:

```julia
a = ArbFloat(2, digits=200)
typeof(a)
# ArbFloat{691}
typeof.(promote(a, ArbFloat(0.0)))
# (ArbFloat{128}, ArbFloat{128})

```

I would expect that `ArbFloat(0.0)` would be promoted to `ArbFloat{691}`. Why is it not so?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 16, 2023, 10:23am UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839/2 "2023-08-16T10:23:56Z")

</div>

It is implemented to do so:

```julia
promote_rule(::Type{ArbFloat{P}},::Type{ArbFloat{Q}}) where {P,Q} = P<Q ? ArbFloat{P} : ArbFloat{Q}

```

in `libarb\promote.jl`

@JeffreySarnoff maybe willing to answer the reasoning about it.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 16, 2023, 10:30am UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839/3 "2023-08-16T10:30:23Z")

</div>

My guess is: results of operations on arguments with different precision should have the lowest precision of all the arguments. Everything else would pretend a precision which isn’t there.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [August 16, 2023, 11:58am UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839/4 "2023-08-16T11:58:04Z")

</div>

makes sense to me

---

<div class="post-metadata">

### Author: ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)
#### Post date: [August 16, 2023, 1:08pm UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839/5 "2023-08-16T13:08:03Z")

</div>

You’re right!  
That does make sense.  
I guess that I would like to have the lower precision number passed with zeros at the end.  
For example, if I have something like this:

```julia
a=ArbFloat(0.2, bits=100)
b=ArbFloat(0.3, bits=1000)
function f(x, y)
    a, b = promote(a, b)
    a * b
end
f(a, b)

```

I would like `a` to be padded with zeros.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [August 30, 2023, 9:27pm UTC](https://discourse.julialang.org/t/promote-on-arbfloat-issue/102839/6 "2023-08-30T21:27:25Z")

</div>

```julia
using ArbNumerics

const ArbFR = Union{ArbFloat, ArbReal}

promote_widen(a, b) = promote(a, b)

function promote_widen(a::ArbFR, b::ArbFR)
   aprec, bprec = precision(a), precision(b)
   if aprec == bprec
      return a, b
   end
   if aprec < bprec
      a = typeof(b)(a)
   else
      b = typeof(a)(b)
   end
   a, b
end

function times(a, b)
     a, b = promote_widen(a, b)
     a * b
end

```

then

```julia
a = 0.3f0
b = 0.7
x = ArbFloat(0.3, bits = 50)
y = ArbFloat(0.7, bits = 100)

ab = times(a, b);
bx = times(b, x);
xy = times(x, y);

a = 0.3f0
b = 0.7
x = ArbFloat(0.3, bits = 50)
y = ArbFloat(0.7, bits = 100)

aa = times(a, a);
ab = times(a, b);
bx = times(b, x);
xy = times(x, y);

```

we obtain

```julia
julia> precision(aa), aa
(24, 0.09f0)

julia> precision(ab), ab
(53, 0.21000000834465027)

julia> precision(bx), bx
(50, 0.21)

julia> precision(xy), xy
(100, 0.209999999999999978905762532122)

```

```julia

```
