Python Forum
cmath.rect accepts a negative modulus
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cmath.rect accepts a negative modulus
#1
The function cmath.rect(r, phi) accepts negative values for r. For example: cmath.rect(-1, 0) returns (-1-0j).

The returned value is according to what is explained in the function documentation: r*(math.cos(phi) + math.sin(phi)*1j), but given that a negative modulus makes no sense, shouldn't the function return an Exception?
Reply
#2
(Jan-17-2024, 07:14 PM)JMB Wrote: given that a negative modulus makes no sense, shouldn't the function return an Exception?
The documentation does not speak about modulus. It speaks about polar coordinates. For example the doctstring
Output:
Help on built-in function rect in module cmath: rect(r, phi, /) Convert from polar coordinates to rectangular coordinates.
It is often accepted in mathematics that polar coordinates allow a negative radius. This is the case when studying plane curves in polar coordinates for example. Interestingly, a way to find the correct modulus and arguments is to compose with cmath.polar()
>>> import cmath
>>> cmath.polar(cmath.rect(-1, 0))
(1.0, -3.141592653589793)  # modulus 1, angle -pi
Should it raise an exception? Well let's say it is a design choice. You can write your own wrapper to raise an exception if the radius is negative!
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thanks for your prompt answer! I think I'll opt for the wrapper, given that I use complex numbers in electrical engineering, to represent A.C. voltages and currents.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  negative memory usage akbarza 1 61 3 hours ago
Last Post: Gribouillis
  function accepts infinite parameters and returns a graph with those values edencthompson 0 868 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  is there any tool to convert negative base to int? Skaperen 7 2,419 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Pygame Rect Not Responding SomebodyImportant 1 1,541 May-02-2021, 01:27 PM
Last Post: BashBedlam
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 4,035 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Def code does not work for negative integers. doug2019 1 1,926 Oct-31-2019, 11:00 PM
Last Post: ichabod801
  offset can not be negative in File.seek()? jollydragon 6 7,022 Sep-28-2019, 03:08 AM
Last Post: jollydragon
  Positive to negative bernardoB 6 4,371 Mar-13-2019, 07:39 PM
Last Post: bernardoB
  Why interpreter accepts indentation made with spaces and not those by tabs sylas 13 5,586 Jan-10-2019, 11:17 PM
Last Post: texadactyl
  Logic of using floor division and modulus for a different variable at different time SB_J 2 2,515 Nov-01-2018, 07:25 PM
Last Post: SB_J

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020