ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 Jul 2022 16:30:31 +0200why there is no exact result with this fractions sum ?https://ask.sagemath.org/question/63152/why-there-is-no-exact-result-with-this-fractions-sum/Hi, here is my code, in order to use it with sagetex (I'm new with sage). I want to make a sum with two fractions.
L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0], 4)
print(L)
print(type(L[0]))
L[0]/L[1]+L[2]/L[3]
and the result is :
[-11, 12, 7, -7]
<class 'int'>
-1.9166666666666665
so, no exact value.
I've made an other test :
L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0],4)
print(L)
print(type(L[0]))
print(L[0]/L[1]+L[2]/L[3])
QQ(L[0]/L[1]+L[2]/L[3])
and the output is :
[-6, -8, -4, 5]
<class 'int'>
-0.050000000000000044
-52215647853572/1044312957071439
instead of -1/20. I would like to understand how to achieve the exact and simplified fraction.
Thank you
-----------------------------------------------
Edit :
I tried with this 1 min ago :
L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0],4)
print(L)
print(type(L[0]))
print(L[0]/L[1]+L[2]/L[3])
print(Integer(L[0])/Integer(L[1])+Integer(L[2])/Integer(L[3]))
QQ(L[0]/L[1]+L[2]/L[3])
and the output is :
[5, -8, -6, -13]
<class 'int'>
-0.16346153846153844
-17/104
-39792720200266/243437817695745
so ... i have to do this with "Integer" ?ryssWed, 06 Jul 2022 16:30:31 +0200https://ask.sagemath.org/question/63152/exact computations with algebraic numbershttps://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/I'm trying to do exact computations with algebraic numbers. In particular, I know to expect integer answers like `1`, `0`, and `3` at the end of my computations, but I'm getting something slightly off.
I noticed that if I run
sage: r = sqrt(2)
sage: a = AA(r)
sage: b = AA(1/r)
sage: c = a*b
Then I get:
sage: c
1.000000000000000?
Is this being handled in the computer as exactly `1`? Otherwise, how can I do exact computations with algebraic numbers in Sage? I obtain the algebraic numbers I'm working with using `algdep()` to find a polynomial and `.roots(QQbar)` to find the roots of that polynomial.b897069Mon, 06 Jul 2020 21:18:17 +0200https://ask.sagemath.org/question/52362/Linear Programming: float solving and exact verificationhttps://ask.sagemath.org/question/48034/linear-programming-float-solving-and-exact-verification/Hi there,
the glpsol tool (GLPK) provides an option "--xcheck": the solver compute the optimal solution uses floating points, and then checks the correctness using exact arithmetics (i guess implicitly they re-check the basis variables, so this only requires one matrix inversation on exact arithmetics)
1. the MILP interface of sage provides the possibility to set, however, it appears that there is no option to activate "xcheck"?
[not allowed to post links but here are the references for the sage documentation] doc.sagemath.org/html/en/reference/numerical/sage/numerical/mip.html#sage.numerical.mip.MixedIntegerLinearProgram.solver_parameter
doc.sagemath.org/html/en/reference/numerical/sage/numerical/backends/glpk_backend.html#sage.numerical.backends.glpk_backend.GLPKBackend.solver_parameter
2. I know that it is easy to extract the basis variables and to recheck the correctness of a floating-point-solution just within sage, however, I really think **this should be added as a basic functionality** for the SAGE MILP interface to ANY solver, not only GLPK (so this verification-code should also verify solutions of cplex,gurobi,etc.) .
So, does anyone have already an implementation for such a verification?
Best,
ManfreddeadalpsSun, 22 Sep 2019 23:33:46 +0200https://ask.sagemath.org/question/48034/