-3

This program bring to random numbers in a decimal way to get sum using ran.next and are stored in the variables sum1a (for the first number) and sum2a (for the second number) and printed in "Suma1.Text"; the operator need to captured the result in the textbox called "Resultado" how has the name "Resultado1" and stored in the variable result1, then is compared with the sum of sum1a and sum2a and stored in total1 to get compared with result1, if total1 match with result1 have to print "Correcto" or "Incorrecto" when there is no match, but sometimes even when the result is correct said that is "Incorrecto".

[Calculator vs Program][1]

namespace Sumas_punto_decimal_prueba
{
public partial class Form1 : Form
{
    double sum1a, sum2a;
    string num1a, num2a;
    double total1;
    double result1;      

    public Form1()
    {
        InitializeComponent();
    }

    private void Generar_Click(object sender, EventArgs e)
    {

        //LIMPIADOR
        foreach (Control ctrl in this.Controls)
        {
            if (ctrl is TextBox)
            {
                TextBox text = ctrl as TextBox;
                text.Clear();
            }
        }

        Random ran = new Random();
        //SUMA 1
        sum1a = ran.Next(100 , 10000) / 100.00;
        sum2a = ran.Next(100 , 10000) / 100.00;
        num1a = sum1a.ToString("##.#0");
        num2a = sum2a.ToString("##.#0");

        if (sum1a < 10 && sum2a < 10)
        {
            Suma1.Text = "   0" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 1 && sum2a < 1)
        {

            Suma1.Text = "  00" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 10 && sum2a < 1)
        {
            Suma1.Text = "   0" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 1 && sum2a < 10)
        {
            Suma1.Text = "  00" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 10)
        {
            Suma1.Text = "   0" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 1)
        {
            Suma1.Text = "  00" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum2a < 10)
        {
            Suma1.Text = "   " + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum2a < 1)
        {
            Suma1.Text = "   " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum2a < 1)
        {
            Suma1.Text = "   " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else
        {
            Suma1.Text = "   " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
    }

    private void Calificar_Click(object sender, EventArgs e)
    {
        //SUMA NUMERO 1
        double.TryParse(Resultado1.Text, out result1);
        total1 = sum1a + sum2a;
        if (result1 == total1)
        {
            Validacion1.Text = "Correcto";
        }
        else
        {
            Validacion1.Text = "Incorrecto. \r\nEl resultado es: " + total1;
        }
    }

}

}

What can be the reason that sometimes that error happen? I make a stimated and happen 1 of 15 times.

Thank to all for the help but already found a solution, using Math.Round() and "Zero placeholder"

        sum1a = ran.Next(10000) / 100.00;
        sum2a = ran.Next(10000) / 100.00;
        sum1a = Math.Round(sum1a, 2, MidpointRounding.AwayFromZero);
        sum2a = Math.Round(sum2a, 2, MidpointRounding.AwayFromZero);
        num1a = sum1a.ToString("00.00");
        num2a = sum2a.ToString("00.00");

            Suma1.Text = "   " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";

Thanks. uwu

  • Your question seems to be both about compacting, and an incorrect result. It might be easier to get an answer if you focus the question only on the incorrect result. You could use Code Review SE for the compacting bit, as it's off topic here. Maybe add some test cases? – Radvylf Programs Sep 24 '19 at 01:49
  • `but some times no` You are going to need to be **much** more specific. – mjwills Sep 24 '19 at 01:52
  • Thiis looks like a complex switch/case case. That control strucutre was given pattern matching a few C# version ago. So you can turn it into something more readable. | In order to tell what is going wrong, we would need to know with what values what result is expected. – Christopher Sep 24 '19 at 02:11
  • Instead of nested if statements just do `else if` to make the code much more readable. – juharr Sep 24 '19 at 02:12
  • 1
    Please read this: [Coding Horror: Flattening Arrow Code](https://blog.codinghorror.com/flattening-arrow-code/) – John Wu Sep 24 '19 at 06:24
  • `if (result1 == total1)` this can get a little tricky when working with [Floating-Point-Arithmetic](https://floating-point-gui.de/). – Fildor Sep 24 '19 at 06:25
  • Also, you sum up the _exact_ values, but you display rounded values. – Fildor Sep 24 '19 at 06:30
  • It seems that you are checking if your numbers are less than 1 only if they are not less than ten. Which obviously will never happen. I suggest ordering your `if` clauses from specific to general. – John Wu Sep 24 '19 at 06:36
  • See also https://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples – Peter Duniho Sep 25 '19 at 01:37

1 Answers1

0

Probably because they don't even equal at all, Probably not all the numbers were captured in. Compare The strings instead and use print action to see where the error is at

if (string == string)
{
// Ganastes!
}

if("159.73" == "159.73")
{
// Correct!
}
Fildor
  • 14,510
  • 4
  • 35
  • 67
Arath
  • 1
  • 2