Forum > General
[SOLVED] Cannot create lookup table with UInt64 values without errors
furious programming:
I need to create some fixed-size lookup table with unsigned 64-bit values, like in the example below:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$LONGSTRINGS ON} const LOOKUP_TABLE: array [0 .. 1] of UInt64 = ($FEDCBA9876543210, $FEDCBA9876543210);beginend.
But the compiler is treating such literals as signed integers and displays warnings about range check errors:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Warning: range check error while evaluating constants (-81985529216486896 must be between 0 and 18446744073709551615)Warning: range check error while evaluating constants (-81985529216486896 must be between 0 and 18446744073709551615)
This example uses default compiler settings and it is compilable (only warnings are emitted). But in my main project, I have custom compiler settings and such a table declaration gives me errors, so the code cannot be compiled at all. To avoid problems, I have to disable range checking and turn off warnings:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$PUSH}{$RANGECHECKS OFF}{$WARNINGS OFF} const LOOKUP_TABLE: array [0 .. 1] of UInt64 = ($FEDCBA9876543210, $FEDCBA9876543210); {$POP}
And such a code can be compiled without errors and warnings. My question is — why the FPC is treating the literals as signed integers, despite the fact that it knows what data type is used by the array?
MarkMLl:
--- Quote from: furious programming on August 25, 2022, 12:54:50 pm ---And such a code can be compiled without errors and warnings. My question is — why the FPC is treating the literals as signed integers, despite the fact that it knows what data type is used by the array?
--- End quote ---
Design decision. It knows that the LHS of the assignment is unsigned but is treating the RHS as signed and then attempting a cast: try putting explicit qword() casts on the RHS numeric literals.
MarkMLl
furious programming:
--- Quote from: MarkMLl on August 25, 2022, 01:03:38 pm ---Design decision. It knows that the LHS of the assignment is unsigned but is treating the RHS as signed and then attempting a cast […]
--- End quote ---
Thank you for the clarification. So this is the design decision.
--- Quote ---[…] try putting explicit qword() casts on the RHS numeric literals.
--- End quote ---
Yep, I can do this to ommit warnings, but my target array contains hundreds of numbers, so if I add explicit casting, its implementation will be super long — that's why I'm trying to reduce the amout of the code needed. Finally, I decided to use compiler directives — short syntax and no warnings and errors.
Again, thanks for the answer.
PascalDragon:
--- Quote from: furious programming on August 25, 2022, 12:54:50 pm ---My question is — why the FPC is treating the literals as signed integers, despite the fact that it knows what data type is used by the array?
--- End quote ---
And here is your problem: Pascal as a language does (in general) not care about the left side, so even if the compiler knows that it parses constants for an unsigned 64-bit integer it must not act upon it, because the right side must be parsed as is and as Pascal as a language prefers signed types the constants are parsed as signed 64-bit integers. Thus there will be complains by the compiler. If you don't want these complains then you need to cast to QWord or UInt64 as MarkMLI mentioned (or disable the message).
MarkMLl:
--- Quote from: furious programming on August 25, 2022, 01:33:00 pm ---Yep, I can do this to ommit warnings, but my target array contains hundreds of numbers, so if I add explicit casting, its implementation will be super long — that's why I'm trying to reduce the amout of the code needed. Finally, I decided to use compiler directives — short syntax and no warnings and errors.
--- End quote ---
Define a macro Q as qword, then it only adds three chars per number.
@PascalDragon Is there any case where there is a risk of a substring match here? If so is there a workaround? I've used QQ as a macro to condense a particular quoting case being used repeatedly in the past, but OP appears vehement about the length aspect.
MarkMLl
Navigation
[0] Message Index
[#] Next page