Announcement

Collapse
No announcement yet.

Float field comparisons when the field value has a non-zero decimal part

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Float field comparisons when the field value has a non-zero decimal part

    Hello

    I am not able to make comparisons with float fields work as I expect when the number has a decimal part equal to zero

    In the attached image I have represented the testing process for this case, which has two tasks
    The first task shows the tests that are done
    The second task generates a record with the values of all the fields involved

    I prepare a record with three float fields with three values 1, 3.5 and 0 which are the ones to be read.
    In the image you can see these values and the definition of these fields (Decimal is the word used in Spanish for float)

    When running the process the comparison works correctly only Click image for larger version

Name:	Float Field Comparison.jpg
Views:	378
Size:	60.6 KB
ID:	62556
    What am I doing wrong?


  • #2
    Hi there,
    Recently I did face the same issue. As a workaround I used a condition like this:
    Code:
    ifThen(
        $prLGBoxArmarioQ < 0.01,
        $prTest3 = '$prLGBoxArmarioQ is 0.0 '
    );

    Comment


    • #3
      Could be that in database it's stored not with zero decimal part but with something like: .000001. You can use rounding function before a comparison.

      Comment


      • #4

        Hello.

        Thanks Maximus and yuri kuzn for your answers.

        No, I don´t think that the issue is having a very small number instead of a zero.

        Let me start explaining how the data was stored.

        Those three fields have a definition that, by default, stores a zero.

        Nevertheless in the tests I have manually stored the three values in them:, 0, 1 and 3.5

        I have prepared a set of tests in the BPM and have printed the output for them.

        First of all I have retrieved their values as follows:

        // Get a valid Id for the testings.
        $prRecordId = '5f54a06368480a801';

        // Retrieve the values
        $prLGBoxDocumQ = record\attribute('LGServicio', $prRecordId, 'qCajaDocum');
        $prLGBoxCofreQ = record\attribute('LGServicio', $prRecordId, 'qCajaCofre');
        $prLGBoxArmarioQ = record\attribute('LGServicio', $prRecordId, 'qCajaArmario');

        As said, after the execution I print the values of the variables involved. The output obtained was:
        $prRecordId: 5f54a06368480a801
        $prLGBoxDocumQ: 0
        $prLGBoxCofreQ: 1
        $prLGBoxArmarioQ: 3.5

        So far, that is correct.

        Then I did the first test. Just compare the retrieved values with the value I have stored in the DB. This is the code used:

        // FIRST GROUP OF TEST
        // Plain initial comparisons

        // Initialize test output
        $prTest1_1 = 'Not modified';
        $prTest1_2 = 'Not modified';
        $prTest1_3 = 'Not modified';
        $prTest1_4 = 'Not modified';

        // Do testings
        ifThen($prLGBoxDocumQ == 0.0,
        $prTest1_1 = '$prLGBoxDocumQ is 0.0 ');
        ifThen($prLGBoxDocumQ == 0,
        $prTest1_2 = '$prLGBoxDocumQ is 0 ');
        ifThen($prLGBoxCofreQ == 1,
        $prTest1_3 = '$prLGBoxCofreQ is 1 ');
        ifThen($prLGBoxArmarioQ == 3.5,
        $prTest1_4 = '$prLGBoxArmarioQ is 3.5 ');

        I expect that all the $prTest1_X changed their ‘Not modified’ value. But this was the output:

        $prTest1_1: Not modified
        $prTest1_2: Not modified
        $prTest1_3: Not modified
        $prTest1_4: $prLGBoxArmarioQ is 3.5

        The only one that worked correctly was the one with a number different from zero behind the decimal sign. This is something that has appeared several times during my tests. I feel it absurd, but is what I saw.

        I thought that instead of zero, maybe there is a very small number. So I prepared this test:

        // SECOND GROUP OF TEST
        // Viewing the data origin

        // Data calculation
        $prVerif2_1 = 1000*$prLGBoxDocumQ;
        $prVerif2_2 = 1000*$prVerif2_1;
        $prVerif2_3 = 1000*$prVerif2_2;
        $prVerif2_4 = 1000*$prVerif2_3;
        $prVerif2_5 = 1000*$prVerif2_4;
        $prVerif2_6 = 1000*$prVerif2_5;
        $prVerif2_7 = 1000*$prVerif2_6;

        And the output was this:

        $prVerif2_1: 0
        $prVerif2_2: 0
        $prVerif2_3: 0
        $prVerif2_4: 0
        $prVerif2_5: 0
        $prVerif2_6: 0
        $prVerif2_7: 0

        So, seems there is no small value. Seems like a real zero.

        In the same sense, I made a different Test using the round formula. If there is something small this should solve that

        // THIRD GROUP OF TEST
        // Rounding data

        $prTest3_1 = 'Not modified';
        $prTest3_2 = 'Not modified';
        $prTest3_3 = 'Not modified';

        $prVerif3_1 = number\round($prLGBoxDocumQ, 6);
        $prVerif3_2 = number\round($prLGBoxDocumQ, 2);
        $prVerif3_3 = number\round($prLGBoxDocumQ, 0);

        ifThen($prVerif3_1 == 0,
        $prTest3_1 = '$prVerif3_1 is 0 ');
        ifThen($prVerif3_2 == 0,
        $prTest3_2 = '$prVerif3_2 is 0 ');
        ifThen($prVerif3_3 == 0,
        $prTest3_3 = '$prVerif3_3 is 0 ');

        And the output obtained was

        $prTest3_1: Not modified
        $prTest3_2: Not modified
        $prTest3_3: Not modified
        $prVerif3_1: 0
        $prVerif3_2: 0
        $prVerif3_3: 0

        Finally I made a last, fourth, test. It looked like a zero but wasn’t a zero. Can I operate with them.
        So I did the following calculations

        // FOURTH GROUP OF TEST
        // Doing some calculations

        $prTest4_1 = 'Not modified';
        $prTest4_2 = 'Not modified';
        $prTest4_3 = 'Not modified';
        $prTest4_4 = 'Not modified';

        $prVerif4_1 = $prLGBoxDocumQ + 0;
        $prVerif4_2 = $prLGBoxDocumQ + 1;
        $prVerif4_3 = $prLGBoxDocumQ * 0;
        $prVerif4_4 = $prLGBoxDocumQ + $prLGBoxCofreQ + $prLGBoxArmarioQ;

        // Do testings
        ifThen($prVerif4_1 == 0,
        $prTest4_1 = '$prVerif4_1 is 0 ');
        ifThen($prVerif4_2 == 1,
        $prTest4_2 = '$prVerif4_2 is 1 ');
        ifThen($prVerif4_3 == 0,
        $prTest4_3 = '$prVerif4_3 is 0 ');
        ifThen($prVerif4_4 == 4.5,
        $prTest4_4 = '$prVerif4_4 is 4.5 ');

        And the output obtained was the following:

        $prTest4_1: Not modified
        $prTest4_2: Not modified
        $prTest4_3: Not modified
        $prTest4_4: $prVerif4_4 is 4.5
        $prVerif4_1: 0
        $prVerif4_2: 1
        $prVerif4_3: 0
        $prVerif4_4: 4.5

        The only one that worked properly was when the decimal part was different from zero.

        I ran the same test with other input data. I concluded that everything works properly when there is a number with a decimal part different from zero.

        Anyway you can reproduce it. I have done it in the demo environment and obtained the same results..

        Comment


        • #5
          It would be easier if you could simplify the test to a few lines of code.

          Comment


          • #6
            As a workaround you can use an additional check. I suspect the issue is that 1.0 is parsed as an integer and then PHP compares integer with a float. I will fix the parsing issue.

            Comment


            • #7
              This fix should help: https://github.com/espocrm/espocrm/c...81da1809975655

              Comment


              • #8
                Originally posted by yurikuzn View Post
                It would be easier if you could simplify the test to a few lines of code.
                I know. I'm allways struggling with that.

                If I reduce it I tend to think that you could try to test something that I already tested

                And If I make it long gets boring and time consuming.

                Apologies for the disturbance

                Comment


                • #9
                  Originally posted by yurikuzn View Post
                  I imagine this will be included in the next release. Do you have a planned date for that?

                  Comment


                  • #10
                    Originally posted by esendino View Post

                    I imagine this will be included in the next release. Do you have a planned date for that?
                    Not sure when it will be released. Could be in one month.

                    I suggest manually replace the file https://raw.githubusercontent.com/es...ula/Parser.php and try.
                    Last edited by yuri; 09-16-2020, 06:47 PM.

                    Comment


                    • #11
                      Thanks a lot!

                      Comment


                      • #12
                        hi yuri I,m still finding the same type of error in version 6.0.8 and 2.6.5 for the advance pack.

                        I have this instruction set

                        ifThenElse($prMoveFwd,
                        $prMonth = $prMonth + 1,
                        $prMonth = $prMonth - 1);

                        // This comparison is generating errors
                        IfThen($prMonth == 13,
                        $prMonth = 1;
                        $prYear = $prYear + 1);

                        IfThen($prMonth == 0,
                        $prMonth = 12;
                        $prYear = $prYear - 1);


                        And the variable $prMonth gets value 12, 13, 14, 15 and so on. Never recognizes its equal to 13

                        Comment


                        • #13
                          In case someone face the same issue, I was able to solve this using number\floor. This is how:

                          // This comparison is generating errors
                          // tried before withour success
                          // IfThen($prMonth== 13,
                          // IfThen(number\round($prMonth) == 13,
                          //

                          IfThen(number\floor($prMonth)== 13,
                          $prMonth = 1;
                          $prYear = $prYear + 1);

                          Anyway I don't feel this is a good solution as maybe someday the variable $prMonth may have an apparent value of 8. And if ask for the (number\floor($prMonth) it will return a 7.

                          Comment

                          Working...
                          X