i am using the book book beginning java 2. and there is a example showing how the math class works
the program calculates the radius of a circle in feet and inches given that the area is 100 square feet:
the output will be 5 feet 8 inches.
my question is why bother with using 'int', why not simply use 'double' for all your variables?
in feet and inches 'int' has been used as the result would have been a floating value. so casting as been used. But doesnt that complicate things?cant one just use long for all variables and forgot about worrying whether the value will fit or not.
Of course, since the values are all calculated in doubles, you won't get any more precision by converting to ints. I think usually it is a performance issue, since mathematical operations are typically an order of magnitude faster for ints than for doubles. In this particular case, it won't make a difference, since all you are doing is displaying the ints. If you were to use the values of feet and inches for a large number of calculations, you would notice a significant difference.
Well, the size of the performance difference will depend heavily on the architecture of your system. The best way to find out is just to experiment!
Try something like this:
This is just a simplistic example to show the difference between double and integer multiplication. I tried to make sure the only difference is in the multiplication operation itself, so I declared both time vars before the first test, removing any overhead for alotting them. Make sure you declare the variables volatile so the compiler won't optimize them out of the code.
I can't guarantee that the code will compile. I don't have a JVM here at work, so I won't be able to debug it. If it doesn't work, the bugs should be fairly easy to find though.
What it does is, saves the time when it is about to go into the first loop, does a thousand double multiplications and then gets the time again. Then it subtracts the start time from the end time, which gives the total time spent in the loop (plus a small overhead for getting the time. Then it prints out the result before doing the same steps for integer multiplication.
You don't really need to know about volatile variables just yet, they basically tell the compiler "watch out, the value can change somewhere outside the code".
Memory can be shared with other threads or other processes. A good example (most often found in C) is when you have peripherals with registers. The peripheral can change the value of the register, so you have to be careful when you write something to the register and then execute, for example, an if based on the value of the register. If you don't declare the identifier as volatile, your compiler could very well say "well, you just set that to x, so the if will always evaluate to false" and then get rid of the if block altogether!
1. Windows XP (on my system, anyway) has a granularity of 10 ms, so anything that completes within the same 10 ms slot in which it starts will get the same end time as the start time, resulting in a measured time of 0.
2. I upped the number of multiplications into the 10 millions, and saw that (again, on my system) the double operations did not take longer than the int operations. So I guess I told you a lie at the beginning regarding floating point being noticably slower than int math.
i'm late to the party, but let me chime in with my 2 cents...
first, your original code will always output 0. You set the area to 0, and then compute the radius from that. any circle with an area of 0 will have a radius of 0.
Now, the reason i'd use an int for feet is that it doesn't make sense to use a float here. 25.83299820 inches is EXACTLY 2 feet, 1.83299820 inches. Why would i want to deal with getting 2.0000000001 feet, and then using the formatter to make that display as 2.
The same logic can be applied to inches. Perhaps, for this application, our degree of tolerance is +- 0.5 inches... in other words, as long as we are within 1/2 an inch, we're close enough. therefore, i don't NEED all that decimal noise. [ June 17, 2005: Message edited by: fred rosenberger ]
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Time for 10,000,000 multiplications (double): 40 Time for 10,000,000 multiplications (int): 40
The total running time of 40ms is way too short to be meaningful. Additionally, for the double multiplication test, you made it too easy: mutiplying by 5.0 is pretty much the same as multiplying by an integer, isn't it? Try to add a fraction to the number and you will see the difference. I adjusted your code (see new version below), ran the test, and here is the output:
The difference between the times in John's test and Timmy's is initializing the variables when the class loads versus assigning to it inside the method.
This makes the double run about 3 times slower and the int about twice as fast. I'm sure this has to do with the fields being volatile. I'm not sure whether volatile is isolating the multiplication operation or obscuring it.
And while I also thought this would make a difference, adding values to the right of the decimal place didn't make any difference in my testing. 4.0 gave the same result as 4.12345, etc.