Reputation: 1801
I'm starting to learn Julia, and I was wondering if there is an equivalent function in Julia that is doing the same as the epsilon function in Fortran.
In Fortran, the epsilon function of variable x
gives the smallest number of the same kind of x
that satisfies 1+epsilon(x)>1
I thought the the function eps()
in Julia would be something similar, so I tried
eps(typeof(x))
but I got the error:
MethodError: no method matching eps(::Type{Int64})
Is there any other function that resembles the Fortran one that can be used on the different variables of the code?
Upvotes: 1
Views: 318
Reputation: 12664
It seems like what you actually want is
eps(x)
not
eps(typeof(x))
The former is exactly equivalent to your Fortran function.
Upvotes: 0
Reputation: 1474
If you really need eps
to work for both Float
and Int
types, you can overload a method of eps
for Int
by writing Base.eps(Int) = one(Int)
. Then your code will work. This is okay for personal projects, but not a good idea for code you intend to share.
Upvotes: 2
Reputation: 14735
As the docstring for eps
says:
help?> eps
eps(::Type{T}) where T<:AbstractFloat
eps()
eps
is only defined for subtypes of AbstractFloat
i.e. floating point numbers. It seems that your variable x
is an integer variable, as the error message says no method matching eps(::Type{Int64})
. It doesn't really make sense to define an eps
for integers since the "the smallest number of the same kind of x that satisfies 1 + epsilon(x) > 1
" is always going to be 1 for integers.
If you did want to get a 1
of the specific type of integer you have, you can use the one
function instead:
julia> x = UInt8(42)
0x2a
julia> one(typeof(x))
0x01
Upvotes: 1