Author Topic: Simple equation totaly wrong  (Read 1844 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Simple equation totaly wrong
« on: December 24, 2014, 02:31:06 pm »
Code: [Select]
int buyCost=4321;
       int sellCost=4252;
       
       int profit=100;
       
       int x=100*profit*sellCost/buyCost;
       profit=x/100;

       System.out.println (profit);
profit=98 and this is the limit.

The next one (100 into 1000)
Code: [Select]
int buyCost=4321;
       int sellCost=4252;
       
       int profit=1000;
       
       int x=1000*profit*sellCost/buyCost;
       profit=x/1000;

       System.out.println (profit);
X=-9

And the further up i go the more it misses.

I am trying to get somewhere at least 4+ digits and because I don't know how to make decimal points I was going this way.
« Last Edit: December 24, 2014, 03:09:20 pm by Kulverstukas »

Offline MainStream

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: Simple equation totaly wrong
« Reply #1 on: December 24, 2014, 04:27:50 pm »
Code: [Select]
int buyCost=4321;
       int sellCost=4252;
       
       int profit=100;
       
       int x=100*profit*sellCost/buyCost;
       profit=x/100;

       System.out.println (profit);
profit=98 and this is the limit.

The next one (100 into 1000)
Code: [Select]
int buyCost=4321;
       int sellCost=4252;
       
       int profit=1000;
       
       int x=1000*profit*sellCost/buyCost;
       profit=x/1000;

       System.out.println (profit);
X=-9

And the further up i go the more it misses.

I am trying to get somewhere at least 4+ digits and because I don't know how to make decimal points I was going this way.


I believe your problem lies in the order of operation. Try putting parentheses around the two integers that are being divided.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Simple equation totaly wrong
« Reply #2 on: December 24, 2014, 05:27:42 pm »
doesn't work. I assume because he first devides and from that I can only get 1 or 0 and not a decimal %.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Simple equation totaly wrong
« Reply #3 on: December 24, 2014, 05:50:02 pm »
If you want to stay with your int x, you would need to do this:

Code: (Java) [Select]
public static void main(String[] args) {
int buyCost = 4321;
int sellCost = 4252;

int profit = 1000;

int x = (int) (1000 * profit * (sellCost / (double) buyCost));
profit = x / 1000;

System.out.println(profit);
}

Otherwise you are performing an integer division of this part:
(sellCost / buyCost)
which will result in 0, because decimal places are just cut.

See the output of this:

Code: (Java) [Select]
int buyCost = 4321;
int sellCost = 4252;
int x = sellCost / buyCost;
System.out.println(x);
Result: 0

This even happens if your x is of the type double, because the right part is evaluated first. So it performes an integer division and converts the 0 int to a double afterwards.

Code: (Java) [Select]
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / buyCost;
System.out.println(x);
Result: 0.0

Only this works as you might expect:

Code: (Java) [Select]
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / (double) buyCost;
System.out.println(x);
Result: 0.984031474195788
« Last Edit: December 24, 2014, 05:53:19 pm by Deque »

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Simple equation totaly wrong
« Reply #4 on: December 24, 2014, 05:56:45 pm »
If you want to stay with your int x, you would need to do this:

Code: (Java) [Select]
public static void main(String[] args) {
      int buyCost = 4321;
      int sellCost = 4252;

      int profit = 1000;

      int x = (int) (1000 * profit * (sellCost / (double) buyCost));
      profit = x / 1000;

      System.out.println(profit);
}

Otherwise you are performing an integer division of this part:
(sellCost / buyCost)
which will result in 0, because decimal places are just cut.

See the output of this:

Code: (Java) [Select]
int buyCost = 4321;
int sellCost = 4252;
int x = sellCost / buyCost;
System.out.println(x);
Result: 0

This even happens if your x is of the type double, because the right part is evaluated first. So it performes an integer division and converts the 0 int to a double afterwards.

Code: (Java) [Select]
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / buyCost;
System.out.println(x);
Result: 0.0

Only this works as you might expect:

Code: (Java) [Select]
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / (double) buyCost;
System.out.println(x);
Result: 0.984031474195788

This works.
Thank you very much.