Julia - Check if float value is an integer - julia

Is there a Julian way to check whether a float variable is numerically equal to some integer value?

There is a simple native function in Julia called isinteger()
julia> isinteger(3.0)
true
julia> isinteger(3.1)
false

Related

Fortran epsilon function equivalent in Julia

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?
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.
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
It seems like what you actually want is
eps(x)
not
eps(typeof(x))
The former is exactly equivalent to your Fortran function.

Convert binary to decimal in Julia

I'd like to convert binary to decimal in Julia. It looks like parseint() became deprecated.
Is the below method the best way to do this?
julia> parse(Int,"111",2)
7
Are you starting with a string? Then yes, that's the way. If you're just wanting to write a constant in binary, then it's much easier to just use the 0b111 syntax. By default, it constructs an unsigned integer (which is displayed in hexadecimal), but you can easily convert it to a signed integer with Int(0b111).
julia> 0b110111
0x37
julia> Int(0b110111)
55

Find the next power of two smaller than n in Julia

I need to find the next power of two smaller than a given number in Julia.
i.e. smallerpoweroftwo(15) should return 8 but smallerpoweroftwo(17) should return 16
I have this so far but searching through the string of bits seems a bit hacky to me. Maybe its not ... Any ideas?
function smallerpoweroftwo(n::Int)
2^(length(bits(n)) - search(bits(n), '1'))
end
Thanks!
Edit:
I was mainly thinking is there a more elegant way to do this just using bitwise arithmetic. Or is there a bit length function somewhere like in some other languages?
Julia's standard library has prevpow2 and nextpow2 functions:
help?> prevpow2
search: prevpow2 prevpow prevprod
prevpow2(n)
The largest power of two not greater than n. Returns 0 for n==0, and returns -prevpow2(-n) for negative
arguments.
help?> nextpow2
search: nextpow2 nextpow nextprod
nextpow2(n)
The smallest power of two not less than n. Returns 0 for n==0, and returns -nextpow2(-n) for negative
arguments.
The prevpow2 function should do what you want.
How about this?1
2^floor(Int, log(2,n-1))
1 added exponent to solution after comment by jverzani.

Arbitrary precision exponentiation in Clojure

Is there way to perform arbitrary precision exponentiation in Clojure? I've tried Math/pow and the expt function from clojure.math.numeric-tower, but both will only return limited precision. For example:
(with-precision 100 (expt 2 1/2))
=> 1.4142135623730951
How do I get more digits?
Apfloat for Java provides fast arbitrary precision arithmetic. You can easily use it by adding the following dependency information to your project.clj file, if your project comes with Leiningen.
[org.apfloat/apfloat "1.6.3"]
You can perform arbitrary precision exponentiation in Clojure using Apfloat. For example:
user> (import '(org.apfloat Apfloat ApfloatMath))
org.apfloat.ApfloatMath
user> (-> (Apfloat. 2M 100) (ApfloatMath/pow (Apfloat. 0.5M 100)))
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727
math/expt is likely not the function you are looking for as it returns a double instead of a BigDecimal in this context, and hence ignores your with-precision statement:
Returns an exact number if the base is an exact number and the power is an integer, otherwise returns a double.
user> (type (with-precision 100 (math/expt 2M 1/2)))
java.lang.Double
the answer to this question seems to cover how to get arbitrary precision out of BigDecimal exponentiation. BigDecimal seems not to provide this "out of the box"

Can a square root of a non-integer become an integer due to floating point rounding errors?

In another unrelated Internet forum a question was asked on how to check if a square root of a given number is an integer. Now in and of itself that is a trivial homework question, but I started to wonder if the naïve approach is correct under all circumstances. That is, in pseudocode:
declare x, y as double
input x
y = sqrt(x)
if round(y) = y then
output "Is integer"
else
output "Isn't integer"
Is it possible to enter such an x, that x itself would not be an integer (or an integer which is not a square of another integer) but sqrt(x) would be and integer because of floating point errors?
Yes: when x is on the edge of Machine epsilon.
Consider x = 1.00...0001, where it is still representable in its binary form, not identical to 1.0. A square root of this number will give 1.0, yielding false poitive.
The square root of the next representable floating-point number above 1.0 (nextafter(1.0) in C) could plausibly evaluate to 1.0.
First off, if the numbers are so large that the precision does not extend down to the decimal point, then you'll only get integers, but they're not correct, so I suppose you don't care about that case.
Concerning exact results: This should be fairly easy to test if you have IEE754 floats. Just take a double that is a perfect integral square, increment or decrement its binary representation by one bit, and then check if the square root is an exact integer. The standard floating point operations are required to be exact to 0.5 units in last place, I believe, so it's possible that the integer is actually the correct nearest representable square root.
Of course:
double d = Math.Sqrt(4.000000000000001);
Console.WriteLine(d == 4);
Console.WriteLine(d == 2);
This results in (C#)
False
True
Feeding x as a float like 1+epsilon will of course work. But for a non-square integer it also works given the integer is large enough.
For example (c#)
ulong i = ulong.MaxValue; // 2^64-1, a non square integer.
double s = Math.Sqrt(i); // Very nearly 2^32
bool same = Math.Round(s) == s; // true, s is close enough to 2^32.

Resources