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 5623 times)
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
 
Page Index Toggle Pages: 1 [2] 
Send Topic Send Topic Print Print