Skip to content

Fraction

Overview

A fraction and associated operations.

NOTE: The common version of this class in the PhET Java code base has a number of additional methods. These methods should be ported into this file as needed. Please see edu.colorado.phet.fractions.common.math.Fraction.java in the PhET Java code base.

@author John Blanco @author Chris Malley (PixelZoom, Inc.) @author Sam Reid (PhET Interactive Simulations)

Class Fraction

import { Fraction } from 'scenerystack/phetcommon';

Constructor

new Fraction( numerator : number, denominator : number )

Instance Methods

getValue() : number

Computes the numeric value of the fraction. Floating-point error is not an issue as long as numerator and denominator are integers < 2^53.

isInteger() : boolean

Does this fraction reduce to an integer value?

toString() : string

copy() : Fraction

reduce() : Fraction

Reduces this fraction, modifies the numerator and denominator.

reduced() : Fraction

Creates a reduced instance of this fraction.

isReduced() : boolean

Is this fraction reduced?

equals( fraction : Fraction ) : boolean

Returns whether the two fractions are equal (not whether their reduced values are equal).

isLessThan( fraction : Fraction ) : boolean

Returns whether this fraction has a value that is less than the provided fraction.

abs() : Fraction

Returns the absolute value of this fraction.

set( value : Fraction ) : Fraction

Sets the value of this fraction to the provided fraction.

setToSum( numerator1 : number, denominator1 : number, numerator2 : number, denominator2 : number ) : Fraction

Sets the value of this fraction to the sum of the two fractions: numerator1 / denominator1 + numerator2 / denominator2

add( value : Fraction ) : Fraction

Adds the provided fraction into this fraction (mutates this fraction). The result is NOT reduced, and has a denominator that is the least-common multiple of the 2 denominators.

plus( value : Fraction ) : Fraction

Adds a fraction to this fraction to create a new fraction. The result is not reduced, and has a denominator that is the least-common multiple of the 2 denominators.

subtract( value : Fraction ) : Fraction

Subtracts the provided fraction from this fraction (mutates this fraction). The result is NOT reduced, and has a denominator that is the least-common multiple of the 2 denominators.

minus( value : Fraction ) : Fraction

Subtracts a fraction from this fraction to create a new fraction. The result is not reduced, and has a denominator that is the least-common multiple of the 2 denominators.

multiply( value : Fraction ) : Fraction

Multiplies the provided fraction and this fraction, setting the result into this fraction (mutates). The value is not reduced.

times( value : Fraction ) : Fraction

Multiplies this fraction by another fraction to create a new fraction. The result is not reduced.

divide( value : Fraction ) : Fraction

Divides this fraction by the provided fraction, setting the result into this fraction (mutates). The value is not reduced.

divided( value : Fraction ) : Fraction

Divides this fraction by another fraction to create a new fraction. The result is not reduced.

plusInteger( value : number ) : Fraction

Convenience method. Adds an integer value to this fraction to create a new fraction. The result is not reduced, and the denominator is the same as the denominator of this fraction.

minusInteger( value : number ) : Fraction

Convenience method. Subtracts an integer value from this fraction to create a new fraction. The result is not reduced, and the denominator is the same as the denominator of this fraction.

timesInteger( value : number ) : Fraction

Convenience method. Multiplies this fraction by an integer to create a new fraction. The result is not reduced, and the denominator is the same as the denominator of this fraction.

dividedInteger( value : number ) : Fraction

Convenience method. Divides this fraction by an integer to create a new fraction. This operation affects the value and sign of the denominator only, and the result is not reduced. Careful! Division by zero is allowed here.

toStateObject() : FractionStateObject

Serializes this Fraction instance.

Static Methods

fromInteger( value : number ) : Fraction

Convenience method for constructing a fraction from an integer.

fromDecimal( value : number ) : Fraction

Convert a number into a Fraction

fromStateObject( stateObject : FractionStateObject ) : Fraction

Deserializes a Fraction from PhET-iO state.

Static Properties

ZERO : Fraction

(readonly)

ONE : Fraction

(readonly)

FractionIO : IOType

(readonly)

IOType for Fraction.

Source Code

See the source for Fraction.ts in the phetcommon repository.