LucaMs Expert Licensed User Longtime User Aug 21, 2014 #1 Code: Copy Private Lng As Long Private Int1, Int2 As Int Int1 = Rnd(0x1, 0x7FFFFFFF) Int2 = Rnd(0x1, 0x7FFFFFFF) Lng = Int1 * Int2 Log("Lng = " & Lng) Why it doesn't work well?
Code: Copy Private Lng As Long Private Int1, Int2 As Int Int1 = Rnd(0x1, 0x7FFFFFFF) Int2 = Rnd(0x1, 0x7FFFFFFF) Lng = Int1 * Int2 Log("Lng = " & Lng) Why it doesn't work well?
S sorex Expert Licensed User Longtime User Aug 21, 2014 #2 strange indeed, maybe the multiplying is done by adding and are overflows to negative numbers causing this issue? you can use something like this instead, Luca. Code: Copy Dim r As String For x=0 To 20:r=r&Chr(Rnd(0x30,0x39)):Next log(r) Last edited: Sep 3, 2014 Upvote 0
strange indeed, maybe the multiplying is done by adding and are overflows to negative numbers causing this issue? you can use something like this instead, Luca. Code: Copy Dim r As String For x=0 To 20:r=r&Chr(Rnd(0x30,0x39)):Next log(r)
klaus Expert Licensed User Longtime User Aug 21, 2014 #3 I would have done it like this: Code: Copy Private Int1, Int2, Lng As Long Int1 = Rnd(0x1, 0x7FFFFFFF) Int2 = Rnd(0x1, 0x7FFFFFFF) Lng = Int1 * Int2 Log("Lng = " & Lng) Upvote 0
I would have done it like this: Code: Copy Private Int1, Int2, Lng As Long Int1 = Rnd(0x1, 0x7FFFFFFF) Int2 = Rnd(0x1, 0x7FFFFFFF) Lng = Int1 * Int2 Log("Lng = " & Lng)
S sorex Expert Licensed User Longtime User Aug 21, 2014 #4 does that work? because the intellisense mentions that rnd returns an int Upvote 0
LucaMs Expert Licensed User Longtime User Aug 21, 2014 #5 sorex said: does that work? because the intellisense mentions that rnd returns an int Click to expand... Right. Actually, I no longer use a random number long, anyway it is strange. Your code should work, sorex, thanks (but it should be from 0 to 17). Upvote 0
sorex said: does that work? because the intellisense mentions that rnd returns an int Click to expand... Right. Actually, I no longer use a random number long, anyway it is strange. Your code should work, sorex, thanks (but it should be from 0 to 17).
Erel B4X founder Staff member Licensed User Longtime User Aug 22, 2014 #6 Klaus code will work. When you multiply to ints the result is also an int. This is why you need to declare the variables as Long variables. Upvote 0
Klaus code will work. When you multiply to ints the result is also an int. This is why you need to declare the variables as Long variables.
S sorex Expert Licensed User Longtime User Aug 22, 2014 #7 maybe that hint should be modified to something like "it will return a value to the destination's data type limitations" instead of int. Upvote 0
maybe that hint should be modified to something like "it will return a value to the destination's data type limitations" instead of int.
LucaMs Expert Licensed User Longtime User Aug 22, 2014 #8 sorex said: maybe that hint should be modified to something like "it will return a value to the destination's data type limitations" instead of int. Click to expand... ... and the parameters sholud be "Number", not Int. I tried these two lines: Code: Copy Private iVar As Int iVar = Power(2, 32) + 100 and in my opinion they should return an error instead set the value of iVar to the maximum possible. Upvote 0
sorex said: maybe that hint should be modified to something like "it will return a value to the destination's data type limitations" instead of int. Click to expand... ... and the parameters sholud be "Number", not Int. I tried these two lines: Code: Copy Private iVar As Int iVar = Power(2, 32) + 100 and in my opinion they should return an error instead set the value of iVar to the maximum possible.
LucaMs Expert Licensed User Longtime User Aug 22, 2014 #9 P.S. I've tried using VB Net Dim a As Int32 = 9999999999999999999 It reports error when writing the code! (Overflow) Upvote 0
P.S. I've tried using VB Net Dim a As Int32 = 9999999999999999999 It reports error when writing the code! (Overflow)
udg Expert Licensed User Longtime User Aug 22, 2014 #10 Hi Luca, the problem with Power function is similar to the original one. Power returns a double and adding 100 to it keeps it a double, so right side of assignment has no problem per se. Casting that double to an integer leads to what you find. In my opinion is the "silent casting" that could lead to false alarms; personally I would prefer a warning at compile-time rather than an error at runtime. Umberto Upvote 0
Hi Luca, the problem with Power function is similar to the original one. Power returns a double and adding 100 to it keeps it a double, so right side of assignment has no problem per se. Casting that double to an integer leads to what you find. In my opinion is the "silent casting" that could lead to false alarms; personally I would prefer a warning at compile-time rather than an error at runtime. Umberto
LucaMs Expert Licensed User Longtime User Aug 22, 2014 #11 Ciao Umberto (Ciao should be international and it is more nice... hi remember me "Heil..." !) I do not remember the technical terms in English (and probably not even in Italian) but the casting should be lawful (G.T. excessive adjective) in one direction and "prohibited" (warned) in the other. I mean that a Long can contain an Int but not vice versa, if the content of long is greater than the maximum possible content for an int, this should be warned. ---------- The reason for of the immediate error reporting (VB.Net) may also relate to the fact that it is possible to perform calculations in the Immediate window. I do not know if this is in the versions of B4A next to my (3.20) but it would be enormously helpful. I think, however, would realize that it is very complex. Upvote 0
Ciao Umberto (Ciao should be international and it is more nice... hi remember me "Heil..." !) I do not remember the technical terms in English (and probably not even in Italian) but the casting should be lawful (G.T. excessive adjective) in one direction and "prohibited" (warned) in the other. I mean that a Long can contain an Int but not vice versa, if the content of long is greater than the maximum possible content for an int, this should be warned. ---------- The reason for of the immediate error reporting (VB.Net) may also relate to the fact that it is possible to perform calculations in the Immediate window. I do not know if this is in the versions of B4A next to my (3.20) but it would be enormously helpful. I think, however, would realize that it is very complex.
sirjo66 Well-Known Member Licensed User Longtime User Aug 22, 2014 #12 Ciao LucaMs, se vuoi ricavarti un Long Rnd va bene quello che hai fatto, ma la riga Code: Copy Lng = Int1 * Int2 non ha senso. Int1 dovrebbero essere 32 bit e Int2 gli altri 32 bit, per cui per abbinarli insieme non devi usare la moltiplicazione, ma uno shift combinato poi con un OR Se hai bisogno di maggiori chiarimenti chiedi pure Sergio Upvote 0
Ciao LucaMs, se vuoi ricavarti un Long Rnd va bene quello che hai fatto, ma la riga Code: Copy Lng = Int1 * Int2 non ha senso. Int1 dovrebbero essere 32 bit e Int2 gli altri 32 bit, per cui per abbinarli insieme non devi usare la moltiplicazione, ma uno shift combinato poi con un OR Se hai bisogno di maggiori chiarimenti chiedi pure Sergio
LucaMs Expert Licensed User Longtime User Aug 22, 2014 #13 Beh ho usato un metodo diverso, alla fine; nel senso che proprio non uso un long, ma non per la difficoltà incontrata. Ho capito comunque cosa intendi, ma quella istruzione non dovrebbe dare problemi! Ora la provo con VB.Net. Grazie -------------- I've changed my method, I no longer use a long, but not for that problem. I understand what you mean with a reference to the shift. However, that statement should be no problem. Upvote 0
Beh ho usato un metodo diverso, alla fine; nel senso che proprio non uso un long, ma non per la difficoltà incontrata. Ho capito comunque cosa intendi, ma quella istruzione non dovrebbe dare problemi! Ora la provo con VB.Net. Grazie -------------- I've changed my method, I no longer use a long, but not for that problem. I understand what you mean with a reference to the shift. However, that statement should be no problem.
LucaMs Expert Licensed User Longtime User Aug 22, 2014 #14 In effetti quella moltiplicazione da un valore enorme; chissà dove l'ho preso! Upvote 0