#3782 closed defect (fixed)
Wrong floor of integer
Reported by: | Rüdiger Franke | Owned by: | Lennart Ochel |
---|---|---|---|
Priority: | high | Milestone: | |
Component: | Backend | Version: | |
Keywords: | Cc: | Willi Braun |
Description
The backend replaces mod
with floor
, ignoring that the former exists for integer and real, while the latter only exists for real. See Modelica spec and other programming languages, like C++ std::floor.
See the following example:
model IntegerModulusTest input Integer x = 5; Integer y = 3; Integer m = mod(x, y); end IntegerModulusTest;
Translation with setCommandLineOptions("+d=dumpTransformedModelica")
gives:
/* This is probably not complete. */ model IntegerModulusTest /* system #1 */ Integer m(); equation /* system #1 */ m = x - floor(x / 3, 0) * 3; end IntegerModulusTest;
This causes a cast error with clang
if used in array initializers (gcc
issues a warning):
error: type 'double' cannot be narrowed to 'int' in initializer list
The C++ runtime translates Modelica floor to std::floor -- both languages define it only for real.
The problem could be worked around in the code generation. But it should be solved at the root cause. The backend must not introduce floor
for integers. Either leave mod
(like in the initial system for this example) or replace floor(x / 3, 0)
with (x / 3)
.
Change History (5)
comment:1 by , 9 years ago
Status: | new → accepted |
---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
I merged the pull request successfully and adapted the c runtime. Can we close this ticket or do we need to do any changes to the cpp runtime?
comment:4 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | accepted → closed |
The examples given in the ticket description do now work with the Cpp runtime.
Thanks!
The conversion is performed in FindZeroCrossings.mo. I changed it to keep the
mod
call in the backend (see PR 552). For the c runtime I have also implemented amod
function for integers and reals, respectively.Therewith, translation with
setCommandLineOptions("+d=dumpTransformedModelica");
gives: