![]() ![]() But this is issue is not specific to multiplication. ![]() The rest of the calculation is then converted to type Long. Test 6 works because VBA recognizes that the large constant 1234567890 requires type Long. But when that is multiplied by 60, VBA automatically converts bla*24*2 to a the next "larger" type (Long) to fit the result. Likewise for bla*24*2, because 2880 fits in an Integer. Initially, bla=60 results in a Variant subtype Integer. Test 5 works because variable "bla" is type Variant by default. If you had written LimitTime - (60 * 24) * 2 * 60 without the outer parentheses, you still get an integer overflow because the multiplication has precence, so again the subexpression (60 * 24) * 2 * 60 is evaluated as type Integer. ![]() Test 4 fails because, again, the subexpression ((60 * 24) * 2 * 60) is evaluated as type Integer. If you had written CDbl(60) * 24 * 2 * 60 or more simply 60# * 24 * 2 * 60, all arithmetic would have been performed as type Double (overkill). It is not converted to type Double until CDbl processes its parameter. Test 3 fails because the subexpression 60 * 24 * 2 * 60 is still evaluated as type Integer. The conversion to type Double does not occur until the right-hand value is stored. Test 2 also fails because the type of the right-hand side arithmetic is determined by the types of values on the right-hand side. 'Overflow, although there is a value again the logic seems to transform the multiplication into an integerĭebug.Print 5, Err.Number, Err.Descriptionĭebug.Print 1234567890 - 60 * 24 * blaTwo * 60ĭebug.Print 6, Err.Number, Err.Description 'NO ERROR LimitTime = LimitTime - ((60 * 24) * 2 * 60)ĭebug.Print 4, Err.Number, Err.Description 'Overflow, although forcing a double, also seen as integer 'Overflow, apparently the Debug.Print can only handle an integer (-32k to +32k)ĭebug.Print 2, Err.Number, Err.Descriptionĭebug.Print 3, Err.Number, Err.Description Similarly, you might write CLng(i)*y.īut that is a work-around and it is not the preferred 1, Err.Number, Err.Description Instead, we must write CLng(200)*300 - or use the implicit type Long constant 200& and/or 300&, which can be confusing. The same thing happens if we write CLng(200*300). In your case, the problem arises because the expression i*y is evaluated as type Integer, before it is converted to type Long to be stored into j. More importantly, using type Single can lead to incorrect values when the variables are converted to type Double for example, storing into an Excel cell.Īnd as you learned, using type Integer can lead to "premature" overflows. And even that is debatable, considering the huge main memories, cache lines and secondary storage in modern computers. But that should matter only with humongous arrays. The only benefit might be in memory space. In modern computers, there is no performance benefit to using type Integer and type Single. In fact, in general, always use type Long for integer variables and type Double for non-integer variables. Solution is to declare i and y type Long.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |