Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Math errors in custom control

Math errors in custom control

Scheduled Pinned Locked Moved WCF and WF
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikey_H
    wrote on last edited by
    #1

    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
    
    M 1 Reply Last reply
    0
    • M Mikey_H

      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
      
      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups