Skip to content

Commit

Permalink
factor out usages of Utils.toFixedNumber, #243
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Dec 11, 2020
1 parent 47ef113 commit 5001b2d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
13 changes: 8 additions & 5 deletions js/common/RAPConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const RAPConstants = {
// The range that the each ratio component (antecedent/consequent) value can be
TOTAL_RATIO_COMPONENT_VALUE_RANGE: new Range( 0, 1 ),

// Consistent way to fix numbers. This should only be used in the view for comparison and display, not in the model, see https://github.com/phetsims/ratio-and-proportion/issues/243
toFixed: x=> Utils.toFixedNumber( x,6),

/**
* Handle keyboard input in a consistent way across all usages of keyboard input to the ratio. This function is
* responsible for making sure that keyboard input snaps to
Expand All @@ -46,21 +49,21 @@ const RAPConstants = {

const snappingFunction = ( newValue, oldValue, useShiftKeyStep ) => {
// Don't conserve the snap for page up/down or home/end keys, just basic movement changes.
const applyConservationSnap = Utils.toFixedNumber( Math.abs( newValue - oldValue ), 6 ) <= shiftKeyboardStep;
const applyConservationSnap = RAPConstants.toFixed( Math.abs( newValue - oldValue ), 6 ) <= shiftKeyboardStep;

if ( remainder === 0 ) {
const snapToKeyboardStep = useShiftKeyStep ? shiftKeyboardStep : keyboardStep;
newValue = Utils.toFixedNumber(
newValue = RAPConstants.toFixed(
Utils.roundSymmetric( newValue / snapToKeyboardStep ) * snapToKeyboardStep,
Utils.numberOfDecimalPlaces( snapToKeyboardStep ) );
}

if ( applyConservationSnap ) {

let returnValue = newValue;
const target = Utils.toFixedNumber( getIdealValue(), 6 );
const target = RAPConstants.toFixed( getIdealValue(), 6 );
if ( newValue > target !== oldValue > target && oldValue !== target ) {
remainder = Utils.toFixedNumber( newValue - target, 6 );
remainder = RAPConstants.toFixed( newValue - target, 6 );
returnValue = target;
}

Expand All @@ -70,7 +73,7 @@ const RAPConstants = {
returnValue = newValue;
}

returnValue = Utils.toFixedNumber( returnValue, 6 );
returnValue = RAPConstants.toFixed( returnValue, 6 );
assert && assert( !isNaN( returnValue ) );

return returnValue;
Expand Down
5 changes: 2 additions & 3 deletions js/common/model/RAPModelTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import Utils from '../../../../dot/js/Utils.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import RAPConstants from '../RAPConstants.js';
import RAPModel from './RAPModel.js';
Expand Down Expand Up @@ -40,7 +39,7 @@ QUnit.test( 'keyboard always can get in proportion: 2/7 moving down', assert =>

newValue = snapConserveFunction( .11, ratioTupleProperty.value.antecedent, true );
ratioTupleProperty.value.antecedent = newValue;
const idealRatio = Utils.toFixedNumber( model.targetRatioProperty.value * ratioTupleProperty.value.consequent, 6 ); // to prevent rounding errors
const idealRatio = RAPConstants.toFixed( model.targetRatioProperty.value * ratioTupleProperty.value.consequent, 6 ); // to prevent rounding errors
assert.ok( ratioTupleProperty.value.antecedent === idealRatio, 'another step down should snap to in proportion' );

newValue = snapConserveFunction( ratioTupleProperty.value.antecedent - .01, ratioTupleProperty.value.antecedent, true );
Expand Down Expand Up @@ -74,7 +73,7 @@ QUnit.test( 'keyboard always can get in proportion: 2/7 moving up', assert => {

newValue = snapConserveFunction( .12, ratioTupleProperty.value.antecedent, true );
ratioTupleProperty.value.antecedent = newValue;
const idealAntecedent = Utils.toFixedNumber( model.targetRatioProperty.value * ratioTupleProperty.value.consequent, 6 ); // to prevent rounding errors
const idealAntecedent = RAPConstants.toFixed( model.targetRatioProperty.value * ratioTupleProperty.value.consequent, 6 ); // to prevent rounding errors
assert.ok( ratioTupleProperty.value.antecedent === idealAntecedent, 'step up through ideal' );

newValue = snapConserveFunction( ratioTupleProperty.value.antecedent + .01, ratioTupleProperty.value.antecedent, true );
Expand Down
6 changes: 3 additions & 3 deletions js/common/view/describers/TickMarkDescriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import Utils from '../../../../../dot/js/Utils.js';
import ratioAndProportion from '../../../ratioAndProportion.js';
import ratioAndProportionStrings from '../../../ratioAndProportionStrings.js';
import RAPConstants from '../../RAPConstants.js';
import TickMarkView from '../TickMarkView.js';

// constants
Expand Down Expand Up @@ -77,10 +77,10 @@ class TickMarkDescriber {
const numberOfTickMarks = this.tickMarkRangeProperty.value;

// account for javascript rounding error
const expandedValue = Utils.toFixedNumber( normalized * numberOfTickMarks, 6 );
const expandedValue = RAPConstants.toFixed( normalized * numberOfTickMarks, 6 );

// account for javascript rounding error
const remainder = Utils.toFixedNumber( expandedValue % 1, 6 );
const remainder = RAPConstants.toFixed( expandedValue % 1, 6 );

assert && assert( remainder < 1 && remainder >= 0, 'remainder not in range' );

Expand Down

0 comments on commit 5001b2d

Please sign in to comment.