Skip to content

Commit

Permalink
round H3O+ and OH- values to 2 non-zero decimal places, #225
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jul 20, 2021
1 parent b98c07b commit aa7c115
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions js/common/PHScaleConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const PHScaleConstants = {

// logarithmic graph
LOGARITHMIC_EXPONENT_RANGE: new Range( -16, 2 ),
LOGARITHMIC_MANTISSA_DECIMAL_PLACES: 1,
LINEAR_EXPONENT_RANGE: new Range( -14, 1 ),
LINEAR_MANTISSA_RANGE: new Range( 0, 8 ),

Expand Down
20 changes: 18 additions & 2 deletions js/common/view/graph/GraphIndicatorDragListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import Utils from '../../../../../dot/js/Utils.js';
import ScientificNotationNode from '../../../../../scenery-phet/js/ScientificNotationNode.js';
import DragListener from '../../../../../scenery/js/listeners/DragListener.js';
import Tandem from '../../../../../tandem/js/Tandem.js';
import phScale from '../../../phScale.js';
Expand Down Expand Up @@ -48,15 +49,30 @@ class GraphIndicatorDragListener extends DragListener {
// Adjust the y-coordinate for the offset between the pointer and the indicator's origin
const y = targetNode.globalToParentPoint( event.pointer.point ).y - clickYOffset;

// Convert the y-coordinate to a model value
// Convert the y-coordinate to a model value.
const value = yToValue( y );
assert && assert( value > 0 );

// Round the model value to the first 2 non-zero decimal places. This prevents continuous dragging from
// creating values that have too much precision, which can result in pH = 7.00 with unequal amounts of
// H3O+ and OH-. See https://github.com/phetsims/ph-scale/issues/225.
const scientificNotation = ScientificNotationNode.toScientificNotation( value, {
mantissaDecimalPlaces: PHScaleConstants.LOGARITHMIC_MANTISSA_DECIMAL_PLACES
} );
const exponent = scientificNotation.exponent - PHScaleConstants.LOGARITHMIC_MANTISSA_DECIMAL_PLACES;
const interval = Math.pow( 10, exponent );
const adjustedValue = Utils.roundToInterval( value, interval );

// Map the model value to pH, depending on which units we're using.
let pH = ( graphUnitsProperty.get() === GraphUnits.MOLES_PER_LITER ) ? concentrationToPH( value ) : molesToPH( value, totalVolumeProperty.get() );
let pH = ( graphUnitsProperty.get() === GraphUnits.MOLES_PER_LITER ) ?
concentrationToPH( adjustedValue ) :
molesToPH( adjustedValue, totalVolumeProperty.get() );

// Constrain the pH to the valid range
pH = Utils.clamp( pH, PHScaleConstants.PH_RANGE.min, PHScaleConstants.PH_RANGE.max );

phet.log && phet.log( `value=${value} adjustedValue=${adjustedValue} pH=${pH}` );

// Set the solution's pH
pHProperty.set( pH );
}
Expand Down
2 changes: 1 addition & 1 deletion js/common/view/graph/GraphIndicatorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GraphIndicatorNode extends Node {
valueYMargin: 3,
xSpacing: 8,
ySpacing: 4,
mantissaDecimalPlaces: 1,
mantissaDecimalPlaces: PHScaleConstants.LOGARITHMIC_MANTISSA_DECIMAL_PLACES,
exponent: null, // use this to request a specific exponent, otherwise the exponent is computed
isInteractive: false,
arrowFill: 'rgb( 0, 200, 0 )',
Expand Down

0 comments on commit aa7c115

Please sign in to comment.