0

I am new to SAGE and am having a problem with something very simple. I have the following code:

delay = float(3.5)
D = delay%1.0
D

But this returns the value -0.5 instead of the expected 0.5. What am I doing wrong?

If I change delay to be delay = float(2.5), I get the right answer, so I don't know why it isn't consistent (I am sure I am using the modulo wrong somehow).

toozie21
  • 341
  • 2
  • 6
  • 15

1 Answers1

2

I think that this question will answer things very well indeed for you.

However, I don't know why you are using float in Sage. Then you could just use Python straight up. Anyway, the % operator is tricky to use outside of integers. For example, here is the docstring for its use on Sage rational numbers.

   Return the remainder of division of self by other, where other is
   coerced to an integer

   INPUT:

   * ``other`` - object that coerces to an integer.

   OUTPUT: integer

   EXAMPLES:

      sage: (-4/17).__mod__(3/1)
      1

I assume this is considered to be a feature, not a bug.

Community
  • 1
  • 1
kcrisman
  • 4,374
  • 20
  • 41
  • Thank you, that was good reading and did explain it. Since I am just trying to get the decimal portion of the value, I changed that section of code to be `D = float(str(delay-int(delay))[1:])`. Also, I was typecasting as floats as an attempt to make modulo work, I have since removed them. – toozie21 Sep 19 '14 at 12:41