c# - Math.Sin 给了我奇怪的值
问题描述
我正在尝试创建一个径向 FAB 菜单,并定义所有圆圈的位置,我有一个理论上可以正常工作的算法,但是当我实际运行它时,Y 值给我一些奇怪的东西。我会很快发布我的代码
请注意,除非您需要参考值的含义,否则这部分并不重要
public class FABContainer : AbsoluteLayout
{
public static int NUMBER_FOR_RING = 1;
public static double ANGLE_INCREMENT = 360.0 / ((double)NUMBER_FOR_RING);
public static double CENTER_RADIUS = 100;
public static double WIDTH_MOD = 0.9;
public FABContainer()
{
this.BackgroundColor = Color.Transparent;
FAB fab = new FAB();
List<InternalFAB> internals = new List<InternalFAB>();
internals.Add(new InternalFAB(this, internals.Count)
{
});
this.Children.Add(fab);
foreach(InternalFAB f in internals) { this.Children.Add(f); };
}
}
public partial class FAB : SuaveControls.Views.FloatingActionButton
{
public FAB()
{
this.HeightRequest = FABContainer.CENTER_RADIUS;
this.WidthRequest = FABContainer.CENTER_RADIUS * FABContainer.WIDTH_MOD;
Debug.WriteLine($"CREATING NEW CENTER WITH W: {this.WidthRequest} H: {this.HeightRequest}");
this.Image = "Edit_Icon.png";
this.BackgroundColor = Color.Transparent;
this.CornerRadius = (int)this.HeightRequest;
}
}
internal class InternalFAB : SuaveControls.Views.FloatingActionButton
{
public static double DIST = 5;
public EventHandler OnClicked;
public static double radius = 80;
public InternalFAB(FABContainer container, int count)
{
this.HeightRequest = (radius);
this.WidthRequest = (radius * FABContainer.WIDTH_MOD * 0.95);
this.BackgroundColor = Color.Transparent;
int index = count + 1;
double AngleDistortion = ((double)index) * FABContainer.ANGLE_INCREMENT;
double xpos = (DIST + this.WidthRequest) * (Math.Cos((Math.PI / 180.0) * AngleDistortion));
double ypos = (DIST + this.HeightRequest) * (Math.Sin((Math.PI / 180.0) *AngleDistortion));
Debug.WriteLine($"CREATING NEW INTERNAL AT: {xpos},{ypos} from distortion {AngleDistortion}");
this.TranslationX = xpos;
this.TranslationY = ypos;
}
}
顺便说一句,这无关紧要,但这是使用 Xamarin。本质上,真正重要并给我带来问题的部分是这部分:
double AngleDistortion = ((double)index) * FABContainer.ANGLE_INCREMENT;
double xpos = (DIST + this.WidthRequest) * (Math.Cos((Math.PI / 180.0) * AngleDistortion));
double ypos = (DIST + this.HeightRequest) * (Math.Sin((Math.PI / 180.0) *AngleDistortion));
Debug.WriteLine($"CREATING NEW INTERNAL AT: {xpos},{ypos} from distortion {AngleDistortion}");
无论出于何种原因,Y 值都返回为-2.08183080149804E-14,而不是在“完美”情况下应该返回的 0。我广泛地浏览了我的代码以试图找到错误无济于事。
如果您需要有关我的代码的更多说明,我会立即告诉您。
解决方案
您正在使用双打,执行浮点运算。这意味着您所做的一切都是近似的。如果你想要精确的值,也许 C# 不是你的 goto 语言。Mathematica 支持这些类型的值。
推荐阅读
- excel - 在包含多个部分的公式中使用错误处理
- tensorflow - 编写自定义优化器问题
- java - java中的大文件下载不完整且没有错误
- cron - jupyter nbconvert 执行到 html cron 作业不起作用
- angular - 如何清除 Angular 反应式表单输入值
- c# - 如何检测图像内容的结束位置?
- python - Gurobi:添加具有下限和上限的约束
- python-3.x - ValueError:无法为具有形状“(?,4)”的张量“Placeholder_36:0”提供形状(4,)的值
- python - Python 3:检查命令是否在“if .. else”块中无异常运行,而不使用“try .. except”
- javascript - 使用外部 Javascript 文件在 html 中制作按钮执行事件