1 Replies - 408 Views - Last Post: 04 September 2011 - 03:49 PM Rate Topic: -----

#1 TheLampard80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-September 11

Rounding to user specific digits

Posted 04 September 2011 - 02:07 PM

Ok, so I have made this code that calculates the roots of a quadratic function... here it is:

 print "ax^2 + bx + c = 0"
def quad_discriminating_roots(a, b, c, entier = 1e-5):
            discriminant = b*b - 4*a*c
            a,b,c,d =complex(a), complex(B)/>, complex(c), complex(discriminant)
            root1 = (-b + d**0.5)/2./a 
            root2 = (-b - d**0.5)/2./a 
            x = root1
            y = root2
            if abs(discriminant) < entier:
                        return "real and equal", abs(x), abs(x)
            if discriminant > 0:
                        return "real", x.real, y.real
            return "complex", x, y
 
for coeffs in ((5, 2, 4/3.), (4, 6, -2), (3, 2, 1), (1.0, -10.0E5, 1.0)):           
            print "Roots of: %gX^2 %+gX %+g are" % coeffs 
            print " %s: %s, %s" % quad_discriminating_roots(*coeffs) 


Now, I want the answers to be rounded to user specific digits (eg.
 "%.4f" % x 
.
I have tried putting this everywhere, but I cannot get it to work. Any thoughts?
Side question, is there any known way of replacing the imaginary unit "j" with the more traditional "i"?

Is This A Good Question/Topic? 0
  • +

Replies To: Rounding to user specific digits

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Rounding to user specific digits

Posted 04 September 2011 - 03:49 PM

This is one of those questions that looks like, if you actually wrote that code, you should be able to answer that question...

So, here's the code you didn't write: http://rosettacode.o...function#Python

And here's you asking the same question around the web:
http://forums.devshe...its-846110.html

You might have had something to do with the one too, but then gave up being subtle: http://www.programmi...post217673.html

Oh, wait, that is you; nothing like username consistency.

Sorry, you have to do some part of your homework on your own. Good luck with that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1