File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange result of Modulo

 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(-9+"%"+-7+"="+-9%-7); // result -2

System.out.println(-7.6+"%"+-2.9+"="+7.6%2.9);// 1.7999999999999998

double d=-7.6;
double e=-2.9;
double f=d%e;
System.out.println(d+"%"+e+"="+f);//-1.7999999999999998

I understand that sign of result is of left operand, but when both operands are negative real numbers, result is +ve. Why?
 
Jhakda Velu
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
well, the code snippet you posted gives a negative result when both operands are negative.
In the second sysout, though you have given a negative sign to print to console, while doing th actual calculation, you missed it, thats why the confusion i think.
Let me know if i understand it properly(or if you ment something else in your problem statement)
jhakda

[ April 09, 2008: Message edited by: Jhakda Velu ]
[ April 09, 2008: Message edited by: Jhakda Velu ]
 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, I missed it
 
shankar reddy
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Example.....
9(Lopenrand) %(Operator) 8(Roperand)

Here what ever the sign of left operand, will added to the result, which is not depend on the right operend. I hope you understand. Reply if you got the point.
 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood where I made a mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic