Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#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.

See:
https://test.openmodelica.org/libraries/PowerSystems_cpp/files/PowerSystems.Examples.Spot.AC3ph.Sensor.err

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 Lennart Ochel, 9 years ago

Status: newaccepted

comment:2 by Lennart Ochel, 9 years ago

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 a mod function for integers and reals, respectively.

Therewith, translation with setCommandLineOptions("+d=dumpTransformedModelica"); gives:

/* This is probably not complete. */
model IntegerModulusTest
  /* system #1 */
  Integer m();
equation
  /* system #1 */
  m = mod(x, 3, 0);
end IntegerModulusTest;

comment:3 by Lennart Ochel, 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 Rüdiger Franke, 9 years ago

Resolution: fixed
Status: acceptedclosed

The examples given in the ticket description do now work with the Cpp runtime.

Thanks!

comment:5 by Martin Sjölund, 7 years ago

Milestone: 1.10.0

Milestone deleted

Note: See TracTickets for help on using tickets.