c# - Math.Sin 给了我奇怪的值(value)观

标签 c# algorithm xamarin math

我正在尝试创建一个径向 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。我广泛地检查了我的代码,试图找出错误,但无济于事。

如果您需要有关我的代码的更多说明,我会立即告诉您。

最佳答案

您正在与 double 合作,表演 floating point arithmetic .这意味着您所做的一切都是近似的。如果您想要精确的值,也许 C# 不是您的 goto 语言。 Mathematica 支持这些类型的值。

关于c# - Math.Sin 给了我奇怪的值(value)观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55110554/

相关文章:

c# - 为什么我不能访问子类中的 protected 变量?

c# - 查询附加实体

c# - 某些 aspx 控件在代码隐藏文件中无法识别

arrays - 面试题: three arrays and O(N*N)

c# - 检查边缘列表中子集的合理快速方法

azure - 如果从另一台计算机构建,UWP 推送通知将停止工作

c# - MonoGame SpriteFont 无法在 iOS 上运行

javascript - C# 和 Javascript 之间的性能和内存差异?

c - 链表在打印时仅显示第一个节点元素

c# - Xamarin.Forms:独立于平台的应用程序菜单