Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Very Hot Topic (More than 25 Replies) @MOD(x,y) Giving Unpredictable Result with Int (Read 5616 times)
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
@MOD(x,y) Giving Unpredictable Result with Int
Mar 25th, 2005 at 7:15pm
Print Post Print Post  
I had to use @MOD functions with Integers variables and came to find out that it gave totally unpredictable results. Sesame clearly mentions in the documents that it should be used with Double and it will give resultant value in double.

Normally one would think

vModevalue = @MOD (9, 2) will result in @Modevalue 1, as both 9 and 2 are integers but not so if you use Int variables.

That makes me wonder what is the difference between the two. I understand that double is used for money value but this seems to be just simple mathematical function and why does it expect to have double? Just wondering and trying to understand the basic differences and usages.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #1 - Mar 25th, 2005 at 8:09pm
Print Post Print Post  
Hello Bharat,

I just tried the following code and it returned a one as expected. What code are you running that is causing unpredictable results?

Code
Select All
Var vf as int
var vl as int

vf = 9
vl = 2

Writeln(@Mod(vf, vl)) 



-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #2 - Mar 25th, 2005 at 9:19pm
Print Post Print Post  
Hi Ray,

I have again tested it with the following code. You will see the different results when vTemp, vFinalTotal and vResults variables are changed to Int. If you will open up those WriteLN, you will see it for yourself the difference. The one with Double as variables test the algorithms successfully while Int do not. Here are some of the valid numbers that pass through the test....023748213, 170657613, 982740722

To pass the algorithm test
10 - vResult = @Tonumber (@Right (RecNumber, 1))


var vTestString as String  
Var i as Int
var vTotal1 as Int
var vTotal2 as Int
var vtemp as Double    
var vFinalTotal as Double  
var vResult as Double      



//number validation for Medicaid - Algorithm



vTestString = @Left (RecNumber, 8 ) 
vTotal1 = 0
vTotal2 = 0
vTemp = 0


For i = 8 downto 2 Step 2
     vTemp =      @int (@Mid (vTestString, i, 1))*2
     
     If VTemp >= 10 then
         {
           vTemp = 1 + @mod (vTemp, 10)
         }
     
     //WriteLn ( "vTemp = " + vTemp)            
       
     vTotal1 = vTotal1 + vTemp
     vTemp = 0

     vTotal2 = vTotal2 + @int (@Mid (vTestString, i-1, 1))
     
     
Next

vFinalTotal = vTotal1 + vTotal2
//WriteLn ("vFinalTotal = "  + vFinalTotal)
vResult = @Mod (vFinalTotal, 10)

//Writeln ("vTestString = " + vtestString + @newline ( ) + "vTotal1 = " + vTotal1 + @newline ( ) + "VTotal2 = " + vTotal2 + @newline ( ) + "vResult = " + vResult )
« Last Edit: Mar 25th, 2005 at 11:16pm by Bharat_Naik »  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #3 - Mar 25th, 2005 at 11:01pm
Print Post Print Post  
Missing some code here?
Quote:
[ //number validation for Medicaid - Algorithm 



vTestString = @Left (RecNumber,   ? )
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #4 - Mar 25th, 2005 at 11:14pm
Print Post Print Post  
Quote:
//number validation for Medicaid - Algorithm  
vTestString = @Left (RecNumber,   ? )


I am sorry. It should be @left (RecNumber, 8 )

It is 9 digits number. The calculation based on first 8 digits that determines the valid ninth digit.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #5 - Mar 27th, 2005 at 1:43am
Print Post Print Post  
This code is used to check the algorithm to see if the number is valid. It checks the nine digit number. First eight digits are used in calculation. You add 1st, 3rd, 5th and 7th digit and add them together. You take 2nd, 4th, 6th and 8th in the other part of calculation, multiply each of this digits by 2 and divide it by 10. Add quotient and remainder to get the value for each of the digits and add them together. Add these both total to get the total value and divide it by 10. 10 minus the remainder should be the last digit for the 9th digit to be valid.

Following Code is being tested:

Quote:
var vTestString as String 
Var i as Int 
var vTotal1 as Int
var vTotal2 as Int 
var vtemp as Int   
var vFinalTotal as Int   
var vResult as Int

Writeln ("The Number being tested for Validity is " + ThisElement)

//number validation for Medicaid - Algorithm


vTestString = @Left (ThisElement, 8 )
     Writeln ("Eight Digits tested in calculation are " + vTestString)
vTotal1 = 0 
vTotal2 = 0 
vTemp = 0 


For i = 8 downto 2 Step 2
     vTemp = 0      
     vTemp = @Tonumber (@Mid (vTestString, i, 1))*2       
       vTemp = @int (vTemp/10) + @MOD (vTemp, 10)                  
     WriteLn ( "vTemp = " + vTemp)         
       vTotal1 = vTotal1 + vTemp
           vTotal2 = vTotal2 + @Tonumber (@Mid (vTestString, i-1, 1)) 
      
      
Next 

     vFinalTotal = vTotal1 + vTotal2
     vResult = @Mod (vFinalTotal, 10) 
Writeln ( "vTotal1 = " + vTotal1 + @newline ( ) + "VTotal2 = " + vTotal2 + @newline ( ) + "FinalTotal = " + vFinalTotal ) 
Writeln ("vResult = " + vResult )
WriteLn ("Last Digit should be " + @str(10 - vResult))



     If @Right (ThisElement, 1) = @str(10 - vResult) then
           {
                 Writeln ("The variables assignment worked Successfully")
           }
         Else
           {
                 Writeln ("The Calculation was Erratic with the variable assigned")
           }



170657613 is a valid number and it should pass through the test if calculation works right.

I shall present here the result of the test I have conducted  while debugging the code.

==========================

When varialbe type assigned as follows:

var vTestString as String 
Var i as Int 
var vTotal1 as Int
var vTotal2 as Int 
var vtemp as Int   
var vFinalTotal as Int   
var vResult as Int

The result of the above code

The Number being tested for Validity is 170657613
Eight Digits tested in calculation are 17065761
vTemp = 2
vTemp = 4
vTemp = 2
vTemp = 4
vTotal1 = 12
VTotal2 = 12
FinalTotal = 24
vResult = 3
Last Digit should be 7
The Calculation was Erratic with the variable assigned
Cry      Cry 
============================

Please look at the value of vTemp variable, it failed to add the Quotient in the statement where @MOD is used.

Logically I thought, I should assign variable type Double where @MOD function is used as programming guide points out.  That made me change last three variables to Double

--------------------------------------------------------

var vTestString as String 
Var i as Int 
var vTotal1 as Int
var vTotal2 as Int 
var vtemp as Double   
var vFinalTotal as Double   
var vResult as Double


The Results of the assignment of above variable type:

The Number being tested for Validity is 170657613
Eight Digits tested in calculation are 17065761
vTemp = 2
vTemp = 5
vTemp = 3
vTemp = 5
vTotal1 = 14
VTotal2 = 12
FinalTotal = 26
vResult = 6
Last Digit should be 4
The Calculation was Erratic with the variable assigned
Cry       Cry
===========================

It messed up when totaling value of vTemp, vTotal value is not totaled up right. It should be 15 but it missed 1 somewhere and gave value of 14, leading to erratic calculation.  I assumed that probably this is due to variable mismatched. vTotal1 being Int type while vTemp1..vTemp4 are Double type.  That lead me to change the other applicable variables to Double as follows.
---------------------------------------------------------

var vTestString as String 
Var i as Int 
var vTotal1 as Double
var vTotal2 as Double 
var vtemp as Double   
var vFinalTotal as Double   
var vResult as Double

The Results of the assignment of above variable type:

The Number being tested for Validity is 170657613
Eight Digits tested in calculation are 17065761
vTemp = 2
vTemp = 5
vTemp = 3
vTemp = 5
vTotal1 = 15
VTotal2 = 12
FinalTotal = 27
vResult = 7
Last Digit should be 3
The variables assignment worked Successfully
=============================
It worked!!!     Smiley

This lead me to following conclusions:

1. @MOD does not work reliably with Int variables.
2. When there are variable type mixing, Double and Int,  in any mathematical statement, it might give unpredictable results.
3. I know very little about variable usage and need a lot of learning to understand Double and Int variables.
4. I need help understanding the fundamentals.
5. Writeln is indispensable for debuging any code.


In the last If....then statement, I had rough time getting the proper result. After converting both the compared to string, it worked correctly.   ???

The above code can be tested by anyone, just have two text elements, any names, just place the code in the first element on Element Exit Event. That's it. It can be tested in preview as it does not have any xfamily statement or function. Place the number to be tested for validity in the first element and hit tab, you will see the result.
The Programming is all about doing.....
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #6 - Mar 27th, 2005 at 3:16am
Print Post Print Post  
I wanted to do this manually before I tackled the programming.  Here are the details of my work.....please confirm that my result is correct from math, and not from coincidence.

Quote:
This code is used to check the algorithm to see if the number is valid. It checks the nine digit number. First eight digits are used in calculation.
A.  You add 1st, 3rd, 5th and 7th digit and add them together.
B.  You take 2nd, 4th, 6th and 8th in the other part of calculation, multiply each of this digits by 2 and divide it by 10.
C.  Add quotient and remainder to get the value for each of the digits and add them together.
D.  Add these both total to get the total value and divide it by 10.
E.  10 minus the remainder should be the last digit for the 9th digit to be valid.


Test Control Number = 170657613

A.  1 + 0 + 5 + 6 = 12

B.  7 * 2 = 14 / 10 = 1.4
    6 * 2 = 12 / 10 = 1.2 
    7 * 2 = 14 / 10 = 1.4 
    1 * 2 =  2 / 10 =  .2
   
C.  1 + 4 = 5   
    1 + 2 = 3      
    1 + 4 = 5
    0 + 2 = 2
   
D.  12 + 5 + 3 + 5 + 2 = 27
    27/10 = 2.7
   
E.  10 - 7 = 3
According to the initial rules, the final answer should be 3, and that is what I got.  Luck or accuracy? 

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #7 - Mar 27th, 2005 at 3:21am
Print Post Print Post  
Bob,
It is not luck. You have done it correctly and methodically. You have understood it very well.

  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #8 - Mar 27th, 2005 at 3:53am
Print Post Print Post  
If my previous message had correct logic, then just using basic math and substitutions, I came up with a single
untested formula for your algorithm.

Code
Select All
var String as String  // test number)
var Digit9 as Int	//(should match last digin of test number)
//-------------------------------------------------------------------
Digit9 = 10 - @Mod(((@TN(@Mid(String,1,1)) +
@TN(@Mid(String,3,1)) +
@TN(@Mid(String,5,1)) +
@TN(@Mid(String,7,1))) +
(@Int((@TN(@Mid(String,2,1))*2/10) +
@Mod((@TN(@Mid(String,2,1))*2/10)) +
 (@Int((@TN(@Mid(String,4,1))*2/10) +
 @Mod((@TN(@Mid(String,4,1))*2/10)) +
 (@Int((@TN(@Mid(String,6,1))*2/10) +
 @Mod((@TN(@Mid(String,6,1))*2/10)) +
(@Int((@TN(@Mid(String,8,1))/*210) +
@Mod((@TN(@Mid(String,8,1))*2/10)))/10)
//------------------------------------------------------------ 


Don't have access to Sesame right now, so the formula above is untested, but should be easy enough for you to test it out?

Edited note: This was posted prior to seeing message that my previous logic was correct.

Edit on 3/28/05.  IGNORE the coding supplied above.  Editing for Sesame showed multiple errors.  Corrected code for a single formula was just posted on Page 2 of this Forum subject a few minutes ago, Reply #21, 3/28/05, 00:50, EDT.
« Last Edit: Mar 28th, 2005 at 6:05am by Bob_Hansen »  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #9 - Mar 27th, 2005 at 4:34am
Print Post Print Post  
In your most recent examples, using Double vs. Integer,  I see that the vTemp values are higher by a value of 1 from Int example and my manual process.

I suspect that this is a result of the random rounding up/down with floating-point calcluations. 

See discussions over past few days about Money Values  Rounding for more information about that issue.

I seem to recall a guideline many years ago, to always use Integers unless you need accuracy to more than 16 decimal places. 
I am sure that Mark can shed some more light on that "guideline".

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #10 - Mar 27th, 2005 at 4:36am
Print Post Print Post  
Bob,
Thank you for your interest and help. The code is made and it has been working fine. My question is pertaining to understanding the Int and Double variables. Why Int variable does not work predicably with @Mod function? Match and mixing of these two variables are allowed or not? What are the fundamentals governing these two variables? Why Int variables would not work with common mathematical calculation in the above code.

Thanks.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #11 - Mar 27th, 2005 at 4:45am
Print Post Print Post  
Quote:
I seem to recall a guideline many years ago, to always use Integers unless you need accuracy to more than 16 decimal places.


Our earlier postings were posted about the same time and hence somewhat disconnected. It is puzzling that the double variables give the desirable results while Int fails to deliver!!!
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #12 - Mar 27th, 2005 at 4:56am
Print Post Print Post  
Looking at your code and your tests, VTotal2 always calculates correctly, but vTotal1 varies.
It looks like the problem is isolated to this one section:
Code
Select All
 For i = 8 downto 2 Step 2
vTemp = 0
vTemp = @Tonumber (@Mid (vTestString, i, 1))*2
vTemp = @int (vTemp/10) + @MOD (vTemp, 10)
vTotal1 = vTotal1 + vTemp   

As I noted in my earlier message, I don't have access to my Sesame right now, but I am curious about the errors you are seeing. 
Where does it first show up as an error?
What if you do WriteLn after of each of those lines? 
What happens if you add @Int to the @TN on the first line that doubles the string value? 

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #13 - Mar 27th, 2005 at 5:00am
Print Post Print Post  
For my own curiosity, can you test out the single formula that I posted earlier? 

Would like to confirm that my logical formula was correct, and would be curiious to see if the problem that you have located shows up with that formula.  Only has one variable and that is Int, no doubles.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #14 - Mar 27th, 2005 at 5:12am
Print Post Print Post  
Yes, I will test it out and report to you the result.
« Last Edit: Mar 27th, 2005 at 6:13am by Bharat_Naik »  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print