A Julia type for representing square roots of rational numbers
Author Jutho
7 Stars
Updated Last
1 Year Ago
Started In
July 2019


Build Status License Coverage Status codecov.io

This package provides a data type RationalRoot{T<:Integer} to exactly represent the (positive or negative) square root of a rational number of type Rational{T}.

Basic usage

RationalRoots can be created from any other real number type by using constructors or convert; if the input is not an integer or rational (i.e. a floating point number), the function rationalize from Julia Base is used to first approximate it by a rational number.

julia> RationalRoot(-2.5)

julia> convert(RationalRoot{Int16}, 7//2)

julia> RationalRoot{BigInt}(2)

Another way of creating a RationalRoot is using the signedroot function, which maps a real number x to sign(x)*sqrt(abs(x)) = x/sqrt(abs(x)). If x is an Integer or Rational, the result is represented as a RationalRoot type. For a floating point number, signedroot will return a floating point number. A RationalRoot output can be forced by using signedroot(<:RationalRoot, x), in which case rationalize is to rationalize the result.

julia> signedroot(3)

julia> signedroot(-4.0)

julia> signedroot(RationalRoot, 5.2)

julia> signedroot(RationalRoot{Int8}, 8)

There is also the inverse function signedsquare, which maps a number x to sign(x)*x^2 = x*abs(x).

julia> signedsquare(1.5)

julia> signedsquare(-2//3)

julia> signedsquare(RationalRoot{BigInt}(1.5))

julia> typeof(ans)

The type RationalRoot will be maintained under multiplication and division with itself or with number of type Integer and Rational. Addition and subtraction require that the type is converted to a floating point number.

Required Packages

No packages found.