Math errors in custom control
-
Control code:
public class MathTester : Control, INotifyPropertyChanged
{
public static DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(double), typeof(MathTester),
new PropertyMetadata(OnValuePropertyChanged));public static DependencyProperty DivisorProperty = DependencyProperty.Register("Divisor", typeof(double), typeof(MathTester), new PropertyMetadata(OnDivisorPropertyChanged)); public static DependencyProperty ResultProperty = DependencyProperty.Register("Result", typeof(double), typeof(MathTester), new PropertyMetadata(OnResultPropertyChanged)); public static DependencyProperty DecimalsProperty = DependencyProperty.Register("Decimals", typeof(int), typeof(MathTester), new PropertyMetadata(15, OnDecimalsPropertyChanged)); public static DependencyProperty RoundProperty = DependencyProperty.Register("Round", typeof(bool), typeof(MathTester), new PropertyMetadata(false, OnRoundPropertyChanged)); public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public double Divisor { get { return (double)GetValue(DivisorProperty); } set { SetValue(DivisorProperty, value); } } public double Result { get { return (double)GetValue(ResultProperty); } set { SetValue(ResultProperty, value); } } public int Decimals { get { return (int)GetValue(DecimalsProperty); } set { SetValue(DecimalsProperty, value); } } public bool Round { get { return (bool)GetValue(RoundProperty); } set { SetValue(RoundProperty, value); } } static MathTester() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MathTester), new FrameworkPropertyMetadata(typeof(MathTester))); } public MathTester() { Divide(); } private void Divide() { if (Value != 0 && Divisor != 0) if (Round == true && Decimals > 0 && Decimals < 16) Result = Math.Round(Value / Diviso
-
Control code:
public class MathTester : Control, INotifyPropertyChanged
{
public static DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(double), typeof(MathTester),
new PropertyMetadata(OnValuePropertyChanged));public static DependencyProperty DivisorProperty = DependencyProperty.Register("Divisor", typeof(double), typeof(MathTester), new PropertyMetadata(OnDivisorPropertyChanged)); public static DependencyProperty ResultProperty = DependencyProperty.Register("Result", typeof(double), typeof(MathTester), new PropertyMetadata(OnResultPropertyChanged)); public static DependencyProperty DecimalsProperty = DependencyProperty.Register("Decimals", typeof(int), typeof(MathTester), new PropertyMetadata(15, OnDecimalsPropertyChanged)); public static DependencyProperty RoundProperty = DependencyProperty.Register("Round", typeof(bool), typeof(MathTester), new PropertyMetadata(false, OnRoundPropertyChanged)); public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public double Divisor { get { return (double)GetValue(DivisorProperty); } set { SetValue(DivisorProperty, value); } } public double Result { get { return (double)GetValue(ResultProperty); } set { SetValue(ResultProperty, value); } } public int Decimals { get { return (int)GetValue(DecimalsProperty); } set { SetValue(DecimalsProperty, value); } } public bool Round { get { return (bool)GetValue(RoundProperty); } set { SetValue(RoundProperty, value); } } static MathTester() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MathTester), new FrameworkPropertyMetadata(typeof(MathTester))); } public MathTester() { Divide(); } private void Divide() { if (Value != 0 && Divisor != 0) if (Round == true && Decimals > 0 && Decimals < 16) Result = Math.Round(Value / Diviso
This gets discussed often, sometimes passionately when the discussion involves someone who absolutely can't believe a simple calculation like 1.2/0.1 can't be exactly represented in a double precision floating point number format. Floating point numbers are only approximations, since there is a finite number of bits to represent an infinite number of - numbers. A couple links of possible interest... What Every Computer Scientist Should Know About Floating-Point Arithmetic[^] Joe Newcomer's Floating Point Explorer[^]
Mark Salsbery Microsoft MVP - Visual C++ :java: