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

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.

``````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?

``````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.