• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular pentagon end result problem  RSS feed

 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Errata link (http://www.cs.armstrong.edu/liang/intro10e/errata.html)

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
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for info and advice
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the answer and can see what will happen. The correct answer is of course
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hah! OK, I misinterpreted that one. Cheers...
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.…
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!