programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Regular pentagon end result problem

Ranch Hand
Posts: 45
Hi everyone,

I am doing the 4.5 exercises which has to do with the area of regular pentagon.

(Geometry: area of a regular polygon) A regular polygon is an n-sided polygon in
which all sides are of the same length and all angles have the same degree (i.e., the
polygon is both equilateral and equiangular). The formula for computing the area
of a regular polygon is :

I have checked errata on their page and they did not list the type-o under the final result.

their result is

Enter the number of sides: 5
Enter the side: 6.5
The area of the polygon is 74.69017017488385

and mine is Area of regular pentagon is 72.69017017488385

is it a code error?

Thanks for assistance

Marshal
Posts: 58295
178
Never use pow to calculate a square. Awlays use x * x. Faster and more precise.

On my calculator, 72.691707…
Must be a misprint.

Lovro Bajc
Ranch Hand
Posts: 45

Campbell Ritchie
Marshal
Posts: 58295
178
You're welcome

Ranch Hand
Posts: 3090
14

Campbell Ritchie wrote:Never use pow to calculate a square. Awlays use x * x. Faster and more precise.

Hm, I have an allergic rection to most statements with "always". Which of these do you suppose would be more precise?

Campbell Ritchie
Marshal
Posts: 58295
178
I know the answer and can see what will happen. The correct answer is of course

Mike Simmons
Ranch Hand
Posts: 3090
14
"The" correct answer? Interesting concept since you're ignoring the two options presented. Also, what if x is 1234567890? Any change to your answer?

Campbell Ritchie
Marshal
Posts: 58295
178
That question is a bit like the Kobiyahsi Maru. You cannot square an int much larger than 34000 because the square root of the maximum value is something above 34000 and you get an overflow error.
You cannot get an int out of the pow method which returns a double.

And no change for 1234567890

Mike Simmons
Ranch Hand
Posts: 3090
14
Not quite the Kobayashi Maru. There is a way (in Java) to square any int and get a result guaranteed to be absolutely precise. Which is not guaranteed when you cast to double, as above.

Actually, there are several ways. One uses only primitive types, and is very fast.

Campbell Ritchie
Marshal
Posts: 58295
178
Do you mean System.out.println(1L * i * i);
?

It is staill a bit like the Kobiyashi Maru. You are bound to win eventually.

Mike Simmons
Ranch Hand
Posts: 3090
14
Well, the Kobayashi Maru is supposed to be a no-win situation. Except for cheating. "You are bound to win eventually" sounds like the complete opposite.

But yes, I was referring to converting the ints to longs, yes. They have more precision than double, and are big enough to handle the square of any int, exactly.

Campbell Ritchie
Marshal
Posts: 58295
178
No, the Klingons always win on the Kobiyashi Maru. Not that I am calling you a Klingon But whatever I do you are going to win.

Mike Simmons
Ranch Hand
Posts: 3090
14
Hah! OK, I misinterpreted that one. Cheers...

Rancher
Posts: 2446
80
hmmm... having just read this topic, and looking up this 'Kobayashi Maru' thing,
I agree with Campbell: it is a lose-lose situation.

First: Mike is right to point out that Campbells quote was a tad on the, eh, very firm
side. But Mike was triggered by the word 'always', and Campbell did not use that word.

But let's hope that OP got the answer he was hoping for.

Greetz,
Piet

Rancher
Posts: 42975
76

But Mike was triggered by the word 'always', and Campbell did not use that word.

Hm, I think "awlays" is close enough to "always" to be considered the same :-)

Piet Souris
Rancher
Posts: 2446
80

Campbell Ritchie
Marshal
Posts: 58295
178

Ulf Dittmer wrote: . . .
Hm, I think "awlays" is close enough to "always" to be considered the same :-)

No, completely different. And I think yes OP did get the answer which is that the area of that pentagon is 72.… not 74.…