【发布时间】:2016-06-23 04:07:36
【问题描述】:
试图解决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
Float 有什么东西可以解释差异还是我的代码本身存在问题?
*********根据建议更新的代码**********************
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)
【问题讨论】:
-
如果您使用与问题描述相匹配的变量,审计会更容易。以
Wf = 1.0为例。 -
更新变量,添加分数函数。现在总和到正确的金额,但个别 BLUE_AREA 仍然关闭??
-
请避免用修改后的版本替换您的原始代码!它使现有答案变得无关紧要,整个问题页面令人困惑。您可以在原始版本之后添加新版本,并说明它是基于某些给定答案和/或 cmets 的修订版本。
-
大部分幻数都可以推导出来。例如,72º 的角度 B 只是圆的 1/5(因为星星有 5 个点),而角度 A 只是 B 的一半。唯一需要的参数是旗帜的纵横比(1.9 fly:葫芦),州的飞行(国旗飞行的40%)和星星的大小(直径= 80%的一条条纹),以及有13条条纹的事实,顶部和底部是红色的,州跨越前 7 名。