簡體   English   中英

無效的操作數到二進制^(有'double'和'double')

[英]invalid operands to binary ^ (have 'double' and 'double')

我對編碼很新,並且已經構建了一個代碼來插入一個三次樣條曲線,但是我堅持使用最后一個等式執行以下錯誤:_“無效操作數到二進制^(有'double'和'double')| “

問題出在代碼的最后一位,兩個第一行的“population =”。 如果有人能指出我正確的方向,我真的很感激。

#include <stdio.h>


main () {

    int x;
    int y;
    double stats[10][2];
    double gpp[8][9] = {0};
    double gppr[10] = {0};
    double year;
    double population;
    int xi = 0;
    year=1950;
    population=0;
    x=0;

    stats[0][0] = 1930; stats[1][0] = 1940; stats[2][0] = 1949;
    stats[3][0] = 1955; stats[4][0] = 1960; stats[5][0] = 1970;
    stats[6][0] = 1980; stats[7][0] = 1990; stats[8][0] = 2000;
    stats[9][0] = 2005;

    stats[0][1] = 21.058; stats[1][1] = 23.547; stats[2][1] = 20.167;
    stats[3][1] = 21.502; stats[4][1] = 24.989; stats[5][1] = 30.852; 
    stats[6][1] = 37.407; stats[7][1] = 43.390; stats[8][1] = 45.985;
    stats[9][1] = 47.041;

    //Initiate  g'' system of equation
    for (x=0;x<8;x++) {
        gpp[x][x] = ((stats[x+1][0]-stats[x][0])+(stats[x+2][0]-stats[x+1][0]))/3;
        if (x<7) {
            gpp[x][x+1] = (stats[x+2][0]-stats[x+1][0])/6;
        }
        if (x>0) {
            gpp[x][x-1] = (stats[x+2][0]-stats[x+1][0])/6;
        }
        gpp[x][8] = ((stats[x+2][1]-stats[x+1][1])/(stats[x+2][0]-stats[x+1][0]))-((stats[x+1][1]-stats[x][1])/(stats[x+1][0]-stats[x][0]));
    }

    //Forward sweep
    for (x=0;x<7;x++) {
        gpp[x+1][x] = 0;
        gpp[x+1][x+1] = gpp[x+1][x+1] - (gpp[x][x+1]/gpp[x][x])*gpp[x+1][x];
        gpp[x+1][8] = gpp[x+1][8] - (gpp[x][x+1]/gpp[x][x])*gpp[x][8];
    }
    //Backward sweep
    gppr[9] = 0;gppr[0] = 0;
    gppr[8] = gpp[7][8]/gpp[7][7];
    for (x=7;x > 0;x=x-1) {
        gppr[x] = (gpp[x][8]-(gppr[x+1]*gpp[x][x+1]))/gpp[x][x];
    }

    //check where is xi
    for (x=0;x<10;x++) {
        if (stats[x][0] > year) {
            xi = x;
            break;
        }
    }

//Calculate population at x
population = (gppr[xi]/6)*((((stats[xi+1][0]-year)^3.0)/(stats[xi+1][0]-stats[xi][0]))-(stats[xi+1][0]-stats[xi][0])*((stats[xi+1][0]-year)))
       + (gppr[xi+1]/6)*((((year-stats[xi][0])^3.0)/(stats[xi+1][0]-stats[xi][0]))-(stats[xi+1][0]-stats[xi][0])*((year-stats[xi+1][0])))
       + (stats[xi][1])*((stats[xi+1][0]-year)/(stats[xi+1][0]-stats[xi][0]))
       + (stats[xi+1][1])*((year-stats[xi][0])/(stats[xi+1][0]-stats[xi][0]));

}

我期待着學習更多關於C的知識!

雨果

^是C中的異或運算符,不適合雙精度運算。 這是位運算符,你可以找到關於它的詳細信息(以及其他位運算符) 這里

如果你想將一個數字提升到另一個數字,你需要pow()函數。

所以類似於:

((stats[xi+1][0]-year)^3.0)

應該寫成:

pow (stats[xi+1][0] - year, 3.0)

7.12.7.4 The pow functions節最新(C11)標准7.12.7.4 The pow functions規定:

概要:
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

描述:
pow函數計算x增加到冪y。 如果x是有限的並且是負的且y是有限的而不是整數值,則會發生域錯誤。 可能會出現范圍錯誤。 如果x為零且y為零,則可能發生域錯誤。 如果x為零且y小於零,則可能發生域錯誤或極點錯誤。

pow函數返回x y

C沒有指數運算符。 ^是XOR運算符,它不適用於非整數,因此出錯。 請改用pow功能。

我想在最后的等式中你試圖找到數學的冪函數。 但在c“^”中,此運算符表示獨占或。 最好試試#include> math.h>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM