Very Hot Topic (More than 25 Replies) @MOD(x,y) Giving Unpredictable Result with Int (Read 5599 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
 
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 #15 - Mar 27th, 2005 at 6:11am
Print Post Print Post  
Code
Select All
var vString as String  // test number)
var Digit9 as Int //(should match last digin of test number)

vString = ThisElement

//-------------------------------------------------------------------
Digit9 = 10 - (@Mod(
@TN(@Mid(vString,1,1)) +
@TN(@Mid(vString,3,1)) +
@TN(@Mid(vString,5,1)) +
@TN(@Mid(vString,7,1)) +
@Int(@TN(@Mid(vString,2,1))/10) +
@Mod(@TN(@Mid(vString,2,1)), 10) +
 @Int(@TN(@Mid(vString,4,1))/10) +
 @Mod(@TN(@Mid(vString,4,1)),10) +
 @Int(@TN(@Mid(vString,6,1))/10) +
 @Mod(@TN(@Mid(vString,6,1)),10) +
@Int(@TN(@Mid(vString,8,1))/10) +
@Mod(@TN(@Mid(vString,8,1)),10), 10))

Writeln ("This should be the last Digit: " + digit9)


 




Adjusted the code as needed to conform with the Sbasic, removed and added a few brackets here and there and removed "/" from @MOD statement and replaced with "," as required by the syntax.

The result does not match... value of digit9 comes 10, will look further.
« Last Edit: Mar 27th, 2005 at 1:09pm by Bharat_Naik »  
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 #16 - Mar 27th, 2005 at 2:11pm
Print Post Print Post  
Code
Select All
var vString as String  // test number)
var Digit9 as Int //(should match last digin of test number)

vString = ThisElement

//-------------------------------------------------------------------
Digit9 = 10 - (@Mod(
@TN(@Mid(vString,1,1)) +
@TN(@Mid(vString,3,1)) +
@TN(@Mid(vString,5,1)) +
@TN(@Mid(vString,7,1)) +
@Int(@TN(@Mid(vString,2,1))*2/10) +
@Mod(@TN(@Mid(vString,2,1))*2, 10) +
 @Int(@TN(@Mid(vString,4,1))*2/10) +
 @Mod(@TN(@Mid(vString,4,1))*2,10) +
 @Int(@TN(@Mid(vString,6,1))*2/10) +
 @Mod(@TN(@Mid(vString,6,1))*2,10) +
@Int(@TN(@Mid(vString,8,1))*2/10) +
@Mod(@TN(@Mid(vString,8,1))*2,10), 10))

Writeln ("This should be the last Digit: " + digit9)

Writeln (@TN(@Mid(vString,1,1))  )
Writeln (@TN(@Mid(vString,3,1))  )
Writeln (@TN(@Mid(vString,5,1)) )
Writeln (@TN(@Mid(vString,7,1))   )
Writeln ( @str(@Int(@TN(@Mid(vString,2,1))*2/10) + @Mod(@TN(@Mid(vString,2,1))*2, 10))   )
Writeln ( @str(@Int(@TN(@Mid(vString,4,1))*2/10) + @Mod(@TN(@Mid(vString,4,1))*2, 10))   )
Writeln ( @str(@Int(@TN(@Mid(vString,6,1))*2/10) + @Mod(@TN(@Mid(vString,6,1))*2, 10))   )
Writeln ( @str(@Int(@TN(@Mid(vString,8,1))*2/10) + @Mod(@TN(@Mid(vString,8,1))*2, 10))   )

 




Calculation of individual digits are right. Logic is also correct. Final answer is still not right. It could be mistake in codeing as the last part of @mod becomes a little complicated.
Moreover, this does not address to usages of variables and why Integer type variable gives different results than double type as this does not use any number kind of variables.
  
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 #17 - Mar 27th, 2005 at 2:16pm
Print Post Print Post  
Quote:
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.



In the above formula one Int variable used but it is used just to take the final value of the calculation. It does not participate in any of the calculations involved.
  
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 #18 - Mar 27th, 2005 at 7:05pm
Print Post Print Post  
Thanks.....spend no more time on it.

I will dig in when I have access to Sesame again.....

At the time I did it, I did not think you had a working solution, I was trying to provide one for you, and you have already resolved that.  And this formula does not directly address the Int/Mod types of issues.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #19 - Mar 27th, 2005 at 11:34pm
Print Post Print Post  
Sorry to have taken so long to get to this thread. @mod does not support integers. The documentation specifies that it accepts only doubles and returns a double. I checked the source code and this is the case. The use of integers will cause at least one rounding or truncation, and possibly more, depending on the number of integers used (up to three).

Unfortunately, while the entry for @mod states that it uses doubles, there is at least one example in the documentation that shows it being used with integers.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
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 #20 - Mar 28th, 2005 at 3:59am
Print Post Print Post  
Mark, thanks a lot for your explanation. Yes, document clearly states that but then I thought it is a simple mathematical calculation so, it should not have problem with Integer. The other thing I have noted that when double and Integer variables are compared in a statement or used in calculations, it does not seem to give predictable result. Need to know a little bit more about these two variables. Once in the past, you advised me to replace Integer variable with double and calculation worked fine after that. I still did not understand why one will work while other would fail to work.
  
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 #21 - Mar 28th, 2005 at 5:54am
Print Post Print Post  
OK, got my Sesame back again......

I made the correct syntax changes to my earlier "single formula" code Reply #8, 04/26/05, 22:53, EDT. 
It appears to be working OK, for the one number you provided..

Code
Select All
var vTestString as String		  // test number)
var vDigit9 as Int			 //(should match last digit of test number)

vTestString = @PromptForUserInput("Enter 8 digits to test","17065761")

vDigit9 = 10 - @Mod (
@TN(@Mid(vTestString,1,1)) +
@TN(@Mid(vTestString,3,1)) +
@TN(@Mid(vTestString,5,1)) +
@TN(@Mid(vTestString,7,1)) +
@Int(@TN(@Mid(vTestString,2,1))*2 /10) +
@Mod(@TN(@Mid(vTestString,2,1))*2,10) +
@Int(@TN(@Mid(vTestString,4,1))*2 /10) +
@Mod(@TN(@Mid(vTestString,4,1))*2,10) +
@Int(@TN(@Mid(vTestString,6,1))*2 /10) +
@Mod(@TN(@Mid(vTestString,6,1))*2,10) +
@Int(@TN(@Mid(vTestString,8,1))*2 /10) +
@Mod(@TN(@Mid(vTestString,8,1))*2,10), 10)

@MsgBox("Last digit after " + vTestString," should be a " + vDigit9,"") 

If I had more time, I would take the 9 digit input and compare the result against the last digit. 

Do you think you could provide me with a dozen or so 9 digit numbers that I can test?  Too lazy to go through the manual process of validation (Obviously why you needed this automated). 

Again, there is only one number variable that is Int, but there are  12 times a String is converted to a Number, and 4 times the Integer value is calculated, and 4 times the  modulo value is calculated.  Still curious to see if the Int/Double problem shows up in this format.


-------------------
Edited to change from *2,10 and *2/10 to *10,2 and *10/2 on the last 8 lines of the central core in the code above.
« Last Edit: Apr 12th, 2005 at 1:43pm 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
 
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 #22 - Mar 28th, 2005 at 6:18am
Print Post Print Post  
982235293, 104875984, 056439797, 017861469, 117036830, 089342307, 081296006, 106250525, 103293056, 145000147, 982089534, 982089526,

Bob, I have a question about getting information from subform using x-family command. Please look at my last post in the following link. Your help, input and feedback are very much appreciated.

http://www.lantica.com/Forum2/cgi-bin/yabb/YaBB.pl?board=gen_disc;action=display...
  
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 #23 - Mar 28th, 2005 at 2:16pm
Print Post Print Post  
Can't look until tonight.......

Thanks for the test numbers
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: @MOD(x,y) Giving Unpredictable Result with Int
Reply #24 - Mar 28th, 2005 at 3:46pm
Print Post Print Post  
Quote:
Mark, thanks a lot for your explanation. Yes, document clearly states that but then I thought it is a simple mathematical calculation so, it should not have problem with Integer. The other thing I have noted that when double and Integer variables are compared in a statement or used in calculations, it does not seem to give predictable result. Need to know a little bit more about these two variables. Once in the past, you advised me to replace Integer variable with double and calculation worked fine after that. I still did not understand why one will work while other would fail to work.

Using integers it is a simple calculation, and given that it is likely to be used with integers, I may well add a @mod command that does use integers.

The reason that things are different when using integers or doubles has to do with two things: accuracy and completeness. An integer is (in Windows) a 32 bit number that is perfectly accurate but very limited in range. A signed integer can only hold numbers from -2,000,000,000 to 2,000,000,000. If an integer exceeds those value in either direction it rolls over. This is because it is a straight binary expression of the numbers 2 to the power of 31, where the 32nd bit indicates if the number is positive or negative. An unsigned integer can range from zero to 4,000,000,000ish. And, of course, an integer cannot hold any fractional portion.

A "double" is an approximation of a "real" number as expressed in scientific notation rendered to a binary form in 64 bits. This gives a double a much larger range than a 32 bit integer, as well as the ability to store a fractional portion - all at the cost of accuracy. That is not to say that doubles are inaccurate unto themselves. With the exception of some early Pentium chips, doubles are accurate to 1.7976931348623157E+308 - which may seem pretty accurate. But that large range degrades on conversion to either integers or to doubles with fewer decimal places (like money).  In either case the double must either be rounded (using IEEE's somewhat strange rounding standards) or truncated to a "whole" number. At each conversion back and forth that reduction in information results in a less accurate double.

If you pass integers to a function or subroutine that is expecting doubles, SBasic does the best it can to convert that integer to a double. It then uses those doubles to do the actual math, and then converts back again to an integer for the result. Each of these conversions - and the case of @mod, the last of these conversions results in the loss of at least 32 of 64 bits worth of information. For example the @mod for 11.56217 % 4.92715 is 1.7078 (or so), yet the integer conversion of the same number is not one or two - it is 11 % 4, which is 3. As you can see, for each of the math operations with a double based @mod, some of the accuracy is truncated for the eventual use as an integer. @mod has a minimum of 3 operations on possibly fractional numbers, and a maximum of five operations (if the first number specified as a parameter is negative).

In this case, the absolute best advise, is to always use the type that the function or subroutine expects - converting only once after you get the result back as a double. That will avoid multiple internal conversions. If you must use integers, then you should either wait for me or Andreas to provide you with a built-in that accepts integers, or you can easily write one for yourself in global code:
Code
Select All
function MyMod(aa as int, bb as int) as int
var cc as int
var dd as int

dd = 0
if(bb <> 0)
{
  cc = aa / bb
  dd = aa - (cc * bb)
}
return dd
end function
 

  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
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 #25 - Mar 28th, 2005 at 4:04pm
Print Post Print Post  
Thanks Mark for your explanation. Your input is very much appreciated. To us it is a final word!!!
  
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 #26 - Mar 28th, 2005 at 4:10pm
Print Post Print Post  
Quote:
Can't look until tonight.......

Thanks for the test numbers


Thanks Bob. No need to look. It works just fine, like any other database or form. This will provide solution to the query FlipGilbert presented in the following link.

http://www.lantica.com/Forum2/cgi-bin/yabb/YaBB.pl?board=gen_disc;action=display...
  
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 #27 - Mar 29th, 2005 at 1:12am
Print Post Print Post  
Results were very disappointing, Result should match last digit of the test numbers:
170657613 got 3 OK
982235293 got 4
104875984 got 4 OK
056439797 got,9
017861469 got 3 ,
117036830 got 0, OK
089342307 got 9 ,
081296006 got 10 ,
106250525 got 2
103293056 got 7
145000147 got 2
982089534 got 5
982089526 got 0

Only 3 of 13 come out correcty.

Will keep this formula and sample numbers to test against Mark's proposed @Mod with Integer capability.
--------------------------------------
Need to add four variables to handle each of the @Mod lines.  No time to play with this tonight......:
------------------------------------
  



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