깃발의 영역 Building Skills with Python을 푸는 시도. 나는 푸른 지역을 해결하려고 노력하고 있는데, 나는 약 2 % 정도 벗어났다. 또한 다른 지역을 합하면 합계가 국기 자체의 면적과 같지 않습니다.파이썬 - 깃발의 영역
import math
def area_star(width):
a = 36.00
b = 72.00
radius_star = 0.0308 * width
a_radians = float(a) * float(math.pi)/float(180)
b_radians = float(b) * float(math.pi)/float(180)
a_sin = math.sin(float(a_radians)/float(2))
b_sin = math.sin(float(b_radians)/float(2))
top = a_sin*b_sin
c_radians = float((a+b)*math.pi)/float(180)
c_sin = math.sin(c_radians)
bottom = 0.5 * c_sin
return 5 * float(top)/float(bottom) * radius_star * radius_star
def fifty_stars(width):
return 50 * area_star(width)
def calculate_areas(width):
WIDTH = width
length = 1.9 * WIDTH
width_union = float(7)/float(13) * WIDTH
length_union = 0.76*WIDTH
NUMBER_RED_STRIPES = 7
NUMBER_WHITE_STRIPES = 6
width_strip_denom = NUMBER_RED_STRIPES+NUMBER_WHITE_STRIPES
width_strip = float(1)/float(width_strip_denom)*WIDTH
blue_area = length_union * width_union - fifty_stars(WIDTH)
white_area = 3 * width_strip * (length*length_union)+3*width_strip*length+fifty_stars(WIDTH)
red_area = 4 * width_strip*(length*length_union)+3*width_strip*length
print 'Our width was given as : %f' %WIDTH
print 'Our length calculates as : %f' %length
print 'Width of our union is: %f' %width_union
print 'Length of our union is: %f' %length_union
print 'Area of a star is %f'%area_star(WIDTH)
print 'Area of 50 stars is %f'%fifty_stars(WIDTH)
print 'Area of our flag in total is : %f '%(WIDTH*length)
print 'Actual WHITE AREA is %f'%white_area
print 'Actual RED AREA is %f'%red_area
print 'Expected BLUE AREA is %f' %(WIDTH*length*.1873)
print 'Actual BLUE AREA is %f'%blue_area
print 'SumofallAreas: %f' % (red_area+white_area+blue_area)
calculate_areas(1.0)
내 출력은 다음과 같습니다 :
Our hoist was given as : 1.000000
Our length calculates as : 1.900000
Width of our union is: 0.538462
Length of our union is: 0.760000
Area of a star is 0.001812
Area of 50 stars is 0.090587
Area of our flag in total is : 1.900000
Actual WHITE AREA is 0.792126
Actual RED AREA is 0.789231
Expected BLUE AREA is 0.355870
Actual BLUE AREA is 0.318644
SumofallAreas: 1.900000
설명 할 수 플로트에 대해 뭔가가 분산 있는가 또는 내 코드 자체에 문제가 다음과 같이 내 코드는?
는 ********* 업데이트 된 코드 PER은
import math
from fractions import Fraction
def area_star(width):
a = 36.00
b = 72.00
radius_star = 0.0308 * width
a_radians = float(a) * float(math.pi)/float(180)
b_radians = float(b) * float(math.pi)/float(180)
a_sin = math.sin(float(a_radians)/float(2))
b_sin = math.sin(float(b_radians)/float(2))
top = a_sin*b_sin
c_radians = float((a+b)*math.pi)/float(180)
c_sin = math.sin(c_radians)
bottom = 0.5 * c_sin
return 5 * float(top)/float(bottom) * radius_star * radius_star
def fifty_stars(width):
return 50 * area_star(width)
def calculate_areas(width):
hoist = width
fly = hoist * Fraction(19,10)
jack_hoist = Fraction(7,13) * hoist
jack_fly = Fraction(76,100)*hoist
NUMBER_RED_STRIPES = 7
NUMBER_WHITE_STRIPES = 6
width_strip_denom = NUMBER_RED_STRIPES+NUMBER_WHITE_STRIPES
width_strip = Fraction(1,width_strip_denom)*hoist
blue_area = jack_fly * jack_hoist - fifty_stars(hoist)
white_area = 3 * width_strip * (fly-jack_fly)+3*width_strip*fly+fifty_stars(hoist)
red_area = 4 * width_strip*(fly-jack_fly)+3*width_strip*fly
print 'Our hoist was given as : %f' %hoist
print 'Our length calculates as : %f' %fly
print 'Width of our union is: %f' %jack_hoist
print 'Length of our union is: %f' %jack_fly
print 'Area of a star is %f'%area_star(hoist)
print 'Area of 50 stars is %f'%fifty_stars(hoist)
print 'Area of our flag in total is : %f '%(hoist*fly)
print 'Actual WHITE AREA is %f'%white_area
print 'Actual RED AREA is %f'%red_area
print 'Expected BLUE AREA is %f' %(hoist*fly*.1873)
print 'Actual BLUE AREA is %f'%blue_area
print 'SumofallAreas: %f' % (red_area+white_area+blue_area)
calculate_areas(1.0)
[? 소수점 연산이 깨진 떠]의 사용 가능한 복제 (http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Aidan
문제점 설명과 일치하는 변수를 사용하면 감사하는 것이 더 쉬울 것입니다. 예를 들어'Wf = 1.0 '이다. –
변수를 업데이트하고 분수 함수를 추가했습니다. 합계를 조정하려면 합계를 합계하지만 BLUE_AREA는 아직 꺼져 있습니다 ?? – Jeeves