rational

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rational defines a rational number, a numerator and denominator, as in EXIF and XMP metadata, such as an exposure time of 1/60 second, which is text such as "1/60", and a number in JSON

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rational

type Rational struct {
	Numerator   int64
	Denominator int64
}

Rational is a signed rational number, as a numerator and a denominator, which isn't reduced, as it's stored, such as 35/10 for f/3.5. A rational with a denominator of zero isn't a number.

func New

func New(numerator, denominator int64) Rational

New returns a rational number, as it is, without reducing it

func Parse

func Parse(text string) (Rational, error)

Parse returns a rational number from text, which is a fraction, such as "35/10" or "-1/4", or a decimal, such as "3.5", reduced to its lowest terms, such as 7/2, or an error when it isn't a number, or its denominator is zero, or it doesn't fit 64 bits

func (Rational) Float64

func (r Rational) Float64() (float64, bool)

Float64 returns the rational number as a float64, or false when its denominator is zero

func (Rational) IsValid

func (r Rational) IsValid() bool

IsValid returns true when the rational number has a denominator, so is a number

func (Rational) MarshalJSON

func (r Rational) MarshalJSON() ([]byte, error)

MarshalJSON returns the rational number as a number, such as 3.5, or null when its denominator is zero

func (Rational) MarshalText

func (r Rational) MarshalText() ([]byte, error)

MarshalText returns the rational number as a fraction, such as "35/10"

func (Rational) String

func (r Rational) String() string

String returns the rational number as a fraction, such as "35/10"

func (*Rational) UnmarshalText

func (r *Rational) UnmarshalText(text []byte) error

UnmarshalText sets the rational number from a fraction, such as "35/10", or a decimal, such as "3.5" (see Parse)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL