# Calculate (I/A)^(2/3) with no allocations with Unitful

**URL:** <https://discourse.julialang.org/t/calculate-i-a-2-3-with-no-allocations-with-unitful/42998>\
**Category:** Performance\
**Created:** [July 13, 2020, 7:28pm UTC](https://discourse.julialang.org/t/calculate-i-a-2-3-with-no-allocations-with-unitful/42998 "2020-07-13T19:28:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [July 13, 2020, 7:28pm UTC](https://discourse.julialang.org/t/calculate-i-a-2-3-with-no-allocations-with-unitful/42998/1 "2020-07-13T19:28:08Z")

</div>

See below. I’m trying to calculate this quantity without allocation. I know that exponentiation and units is tricky since a rational exponent is stored in the type of each unitful quantity, but even when I try to do all the work for the complier it still allocates.

```julia
using Unitful
I = 1.0u"A"
A = 1.0u"A/K^(3/2)"

f1(x,y) = x+y
@btime f1($I, $I) # 2.0 A, zero allocations, so I know allocations are not fundamental to unitful

# I know the exponents of the units are in types and stored as rational, so maybe
# using a constant rational exponent will work
f2(I,A) = (I/A)^(2//3)
@btime f2($I, $A) # 1.0K, 14 allocations
@btime f2(1.0, 1.0) #1.0, 0 allocations, so the allocations are not required for exponentiation

# ok lets just get the number we want as a float, raise it to an exponent, then convert back to the units we know the answer has
f3(I,A) = (Float64((I/A)/u"K^(3/2)")^(2//3))*1u"K"
@btime f3($I, $A) #1.0K, 17 allocations

```

Any idea how I can do this without allocating?

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [July 13, 2020, 10:17pm UTC](https://discourse.julialang.org/t/calculate-i-a-2-3-with-no-allocations-with-unitful/42998/2 "2020-07-13T22:17:00Z")

</div>

```julia
julia> f(I, A) = (z=cbrt(I/A);z*z)
f (generic function with 2 methods)

julia> @btime f($I, $A)
  11.043 ns (0 allocations: 0 bytes)
1.0 K

```

Solves my problem, though it would be nice to be able to write it more like the physical equations.
