There exist at least two different definitions of the modulo operator that lead to different results in case of negative operands; please see https://en.wikipedia.org/wiki/Modulo_operation .
Mathematically Knuth's definition (used in glpk) is most correct. An easy way to avoid any surprises is just not to use negative operands. On Sun, 2021-08-15 at 21:06 +0200, Domingo Alvarez Duarte wrote: > Definitely the way GMPL/GLPK calculates the modulo of a negative > number > is wrong: > > ==== > > param a := 4; > param b := 3; > param c := a mod b; > param d := a - c; > > > param a2 := -4; > param b2 := 3; > param c2 := a2 mod b2; > param d2 := a2 - c2; > > display a, b, c, d, a2, b2, c2, d2; > > ==== > > AMPL output: > > ==== > > ampl test4.ampl > a = 4 > b = 3 > c = 1 > d = 3 > a2 = -4 > b2 = 3 > c2 = -1 > d2 = -3 > > ==== > > GMPL/GLPK/GLPSOL output: > > ==== > > glpsol -m test4.ampl > GLPSOL: GLPK LP/MIP Solver, v4.65-ex, glp_double size 8 > Parameter(s) specified in the command line: > -m test4.ampl > Reading model section from test4.ampl... > test4.ampl:12: warning: unexpected end of file; missing end statement > inserted > 12 lines were read > Display statement at line 12 > a = 4 > b = 3 > c = 1 > d = 3 > a2 = -4 > b2 = 3 > c2 = 2 > d2 = -6 > > ... > > ==== > > Cheers ! > > On 15/8/21 16:50, Heinrich Schuchardt wrote: > > On 8/15/21 4:06 PM, Domingo Alvarez Duarte wrote: > > > Comparing how AMPL and GMPL calculate random expressions I found > > > some of > > > then where they differ or GMPL can't manage see bellow, also one > > > of then > > > that references itself makes glpsol segfault but ampl gives an > > > error > > > > I can't see a segfault with GLPK 5.0. > > > > Please, use gdb to identify in which line of code it occurs: > > > > gdb --args glpk -m test.od > > > > > message explaining the problem: > > > > > > ==== > > > > > > param a0 := (((((((788)*(8.46))))+8342*1.803-1))*4186.4*(15)); > > > printf "%f\n", a0; > > > > > > param a1 := (((22 mod 284/((7530/((2)*(((((25))-421))))))*597 mod > > > 2663)+7283.8-9.60+167 mod ((3))))+(8871); > > > printf "%f\n", a1; > > > > > > #param a2 := a1 * a2; #glpsol segfault > > > param a2 := a0 * a1; > > > printf "%f\n", a2; > > > > > > param a = (((((((788)*(8.46))))+8342*1.803-1))*4186.4*(15))*(((22 > > > mod > > > > Your syntax is wrong. This should be: > > > > param a := > > > > With correct syntax: > > > > glpsol -m /tmp/test.mod > > GLPSOL: GLPK LP/MIP Solver, v4.65 > > Parameter(s) specified in the command line: > > -m /tmp/test.mod > > Reading model section from /tmp/test.mod... > > 28 lines were read > > 1363056632.376000 > > 17428.775299 > > 23756407765226.851562 > > 11664732388290.359375 > > 11665910614443.386719 > > 8259816920615.111328 > > 8259816920615.111328 > > Model has been successfully generated > > > > Best regards > > > > Heinrich > > > > > 284/((7530/((2)*(((((25))-421))))))*597 mod 2663)+7283.8-9.60+167 > > > mod > > > ((3))))+(8871); > > > #param a := 3+2; > > > printf "%f\n", a; > > > > > > param b = (((((((788)*(8.46))))+8342*1.803-1))*4186.4*(15))*(((22 > > > mod > > > 284/((7530/((2)*(((((25))-421))))))*597 mod 2663)+7283.8- > > > 9.60+167.8644 > > > mod ((3))))+(8871); > > > #param a := 3+2; > > > printf "%f\n", b; > > > > > > param c = > > > (((((((788)*(8.46))))+8342*1.803- > > > 1))*4186.4*(15))*(((22/((7530/((2)*(((((25))- > > > 421))))))*597)+7283.8-9.60+167))+(8871); > > > > > > > > > #param a := 3+2; > > > printf "%f\n", c; > > > > > > param d = > > > (((((((788.0)*(8.46))))+8342.0*1.803- > > > 1.0))*4186.4*(15.0))*(((22.0/((7530.0/((2.0)*(((((25.0))- > > > 421))))))*597.0)+7283.8-9.60+167.0))+(8871.0); > > > > > > > > > #param a := 3+2; > > > printf "%f\n", d; > > > > > > ==== > > > > > > AMPL output: > > > > > > ==== > > > > > > ampl test.ampl > > > 1363056632.376000 > > > 14765.775299 > > > 20126587953209.562500 > > > 8034912576273.071289 > > > 8036090802426.097656 > > > 8259816920615.111328 > > > 8259816920615.111328 > > > > > > ==== > > > > > > GLPSOL output: > > > > > > ==== > > > > > > glpsol -m test.ampl > > > GLPSOL: GLPK LP/MIP Solver, v4.65 > > > Parameter(s) specified in the command line: > > > -m test.ampl > > > Reading model section from test.ampl... > > > test.ampl:25: warning: unexpected end of file; missing end > > > statement > > > inserted > > > 25 lines were read > > > 1363056632.376000 > > > 17428.775299 > > > 23756407765226.851562 > > > test.ampl:13: no value for a > > > MathProg model processing error > > > > > > ==== > > > > > > With a incorrect expression: > > > > > > ==== > > > > > > param a2 := a1 * a2; #glpsol segfault > > > #param a2 := a0 * a1; > > > > > > ==== > > > > > > AMPL output: > > > > > > ==== > > > > > > ampl test.ampl > > > 1363056632.376000 > > > 14765.775299 > > > > > > test.ampl, line 7 (offset 228): > > > a2 is not defined > > > context: param a2 := a1 * >>> a2; <<< #glpsol segfault > > > > > > ==== > > > > > > GLPSOL output: > > > > > > ==== > > > > > > glpsol -m test.ampl > > > GLPSOL: GLPK LP/MIP Solver, v4.65 > > > Parameter(s) specified in the command line: > > > -m test.ampl > > > Reading model section from test.ampl... > > > test.ampl:25: warning: unexpected end of file; missing end > > > statement > > > inserted > > > 25 lines were read > > > 1363056632.376000 > > > 17428.775299 > > > Segmentation fault (core dumped) > > > > > > ==== > > > > > > Cheers ! > > > > > > > >
