如何在Java中使用BigDecimal来获得正弦值的准确结果?

问题描述 投票:0回答:1

我的目标是生成准确的正弦波。我的问题是,当我使用BigDecimal和StrictMath生成值时,某些零交叉是错误的并且对称性被破坏了。

这里是一个以1的频率,0的相位,1的振幅,1秒的时间和10的采样率生成的数组(我将在本文的后面发布代码):

>[0]    0.0

>[1]    0.5877852522924731  
[2] 0.9510565162951535  
[3] 0.9510565162951536  
[4] 0.5877852522924732  
[5] 1.2246467991473532E-16  
[6] -0.587785252292473  
[7] -0.9510565162951535 

>[8]    -0.9510565162951536 

>[9]    -0.5877852522924734 

[5]为准确性不应该为0吗? (4 = 1)以及(2 = 3),(9 = 6)和(7 = 8)不应该吗?

[第二种情况,相位等于StrictMath.PI / 2.0似乎在[5]处产生精度:

>[0]    1.0 

>[1]    0.8090169943749475  
[2] 0.3090169943749475  
[3] -0.3090169943749473 

>[4]    -0.8090169943749473 

>[5]    -1.0    
[6] -0.8090169943749476 

>[7]    -0.3090169943749476 

>[8]    0.3090169943749472  
[9] 0.8090169943749472  

在这种情况下,起点不太准确,[5]更准确,但是再一次,不应(-4 = 1)以及(-2 = 3),(-9 = 6)和(-7 = 8)?

所以我的问题是为什么会这样?为什么零交叉点是错误的,而1和-1交叉点是正确的?为什么正弦对称性被破坏?

这是我用于生成值的代码:


    package Wave;

    import java.math.BigDecimal;

    /**
     * @author Alexander Johnston
     * Copyright 2019
     * A class for sine waves
     */
    public class SineWave extends Wave {

        /** Creates a sine wave
         * @param a as the amplitude of the sin wave from -amplitude to amplitude
         * @param f as the frequency of the sine wave in Hz
         * @param p as the phase of the sine wave
         */
        public SineWave(BigDecimal a, BigDecimal f, BigDecimal p) {
            this.a = a;
            this.f = f;
            this.p = p;
        }

        /* (non-Javadoc)
         * @see waves.Wave#getSample(BigDecimal, float)
         */
        public double[] getSample(BigDecimal t, float sr) {
            int nsd;
            BigDecimal nsdp = (new BigDecimal(Float.toString(sr)).multiply(t));
            if(nsdp.compareTo(new BigDecimal(Integer.MAX_VALUE)) == -1) {
                nsd = nsdp.intValue();
                } else {
                    System.out.print("wave time is too long to fit in an array");
                    return null;
                }
            double[] w = new double[nsd];
            for(int i = 0; i < w.length; i++) {
                w[i] = a.multiply(new BigDecimal(StrictMath.sin(((new BigDecimal(2.0).multiply(new BigDecimal(StrictMath.PI)).multiply(f).multiply(new BigDecimal(i)).divide((new BigDecimal(Float.toString(sr))))).add(p)).doubleValue()))).doubleValue();
            }
            p = p.add(new BigDecimal(2.0).multiply(new BigDecimal(StrictMath.PI).multiply(f).multiply(t)));
            return w;
        }
    }


    The wave class:

    package Wave;

    import java.math.BigDecimal;

    /**
     * @author Alexander Johnston
     * Copyright 2019
     * A class for waves to extend
     */
    public abstract class Wave {

        // Amplitude of the wave
        protected BigDecimal a;

        // Frequency of the wave in Hz
        protected BigDecimal f;

        // Phase of the wave, between 0 and (2*Math.PI)
        protected BigDecimal p;

        /** Generates a wave with with the correct amplitude
         * @param t as the length of the wave in seconds
         * @return An array with the wave generated with specified amplitude as amplitude over time
         */
        abstract public double[] getSample(BigDecimal t, float sr);

        }

和主要方法:


    import java.math.BigDecimal;
    import Wave.SineWave;

    public class main {

        public static void main(String[] args) {
            BigDecimal a = new BigDecimal(1.0); 
            BigDecimal f = new BigDecimal(1.0); 
            BigDecimal p = new BigDecimal(0.0);
            SineWave sw = new SineWave(a, f, p);        
            p = new BigDecimal(StrictMath.PI).divide(new BigDecimal(2.0));
            SineWave swps = new SineWave(a, f, p);
            BigDecimal t = new BigDecimal(1.0);
            float sr = 10;
        // The first array in this post
            double [] swdns = sw.getSample(t, sr);
        // The second array in this post
            double [] swpsdns = swps.getSample(t, sr);
        }

感谢您抽出宝贵时间浏览我的帖子。非常感谢您的帮助。

java trigonometry bigdecimal
1个回答
0
投票

按照Erwin的建议,我找到了一个可以满足我的需要的库。BigDecimalMath当我将精度设置为1074个小数位(这是Java基本双精度值的负指数的最大绝对值)时,它具有慷慨的许可证并解决了我对这些特定数组的问题。

再次感谢您对Erwin的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.