• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users




(1) 2 »


Re: 請教C語言如何做開根號運算
#17
資深會員
資深會員


查看用戶資訊
我根據C_H_M 大大的範例,分別代入好幾個常數來驗算反三角函數的的程式,結果都正確,再次感謝C_H_M 大大的熱心幫忙。在Watch視窗中要觀察變數記得要將Size設為24Bit。

發表於: 2008/3/24 9:25
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#16
資深會員
資深會員


查看用戶資訊
感謝高手指點迷津,C_H_M 大大對 Compiler 真是了解,研究程式中.....

發表於: 2008/3/21 17:42
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#15
中級會員
中級會員


查看用戶資訊
在程式前一行加上
[code]

然後在最後一行後再加上
?>
[/code]
這樣程式就不會變樣了. 可以按 "送出" 旁邊的 "預覧觀看" 看一下效果.
(我也是看了別人的文之後才知道的 !)

-----------------------------------------------------------------
反三角函數的部分我再來猜一下.
用你前一個測試程式再加上 atan.c 的函數內容.

// atan.c 中的 PI 定義
#define    PI    3.14159265358979
#define    TWO_PI    6.28318530717958
#define    HALF_PI    1.570796326794895

#include <math.h>
#include <errno.h>

double sqrt_in;
double sqrt_root;

double atan_in;
double atan_r;

unsigned char BUF;
double sqrt(double x);
/*------------------------------------------------------------------------------*/
// MAIN PROGRAM AREA
/*------------------------------------------------------------------------------*/
void main(void)
{
 
atan_in 1;
 
atan_r atan(atan_in) / PI 180;

 
sqrt_in 100;
 
sqrt_root sqrt(sqrt_in);
 if(
sqrt_root == 10)
   { 
BUF=1;
   }
 else
   { 
BUF=0;
   }
}

double
sqrt
(double x)
{
    
double    ogng;
    
short    niter;
    
int    expon;

    if(
0.0) {
        
errno EDOM;
        return 
0.0;
    }
    if(
== 0.0) {
        return 
0.0;
    }
    
og x;
    if(
og 1.0)
        
og 1.0/og;
    
og frexp(og, &expon);
    
og ldexp(ogexpon/2);        /* make an educated guess */
    
if(1.0)
        
og 1.0/og;
    
niter 20;
    do {
        
ng = (x/og og)/2.0;
        if(
ng == og)
            break;
        
og ng;
    } while(--
niter);
    return 
og;
}

// 由 atan.c 中複製而來的程式
double
atan
(double f)
{
    static const 
double    coeff_a[] =
    {
        
33.058618473989548,
        
58.655751569001961,
        
32.390974856200445,
        
5.8531952112628600,
        
0.19523741936234277,
        -
.0024346033004411264
    
};
    static const 
double    coeff_b[] =
    {
        
33.058618473992416,
        
69.675291059524653,
        
49.004348218216250,
        
12.975578862709239,
        
1.0
    
};
    
unsigned char    recip;
    
double        valval_squared;

    if((
val fabs(f)) == 0.0)
        return 
0.0;
    if(
recip = (val 1.0))
        
val 1.0/val;
    
val_squared val val;
    
val *= eval_poly(val_squaredcoeff_a5)/eval_poly(val_squaredcoeff_b4);
    if(
recip)
        
val HALF_PI val;
    return 
0.0 ? -val val;
}


其中要注意 atan() 傳回的是弧度,
要轉成一般人印象中的角度, 需要 atan() / PI * 180 這樣計算.
(式中的意思為, 結果值為幾個 PI , 而一個 PI = 180 度)

發表於: 2008/3/21 17:28
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#14
資深會員
資深會員


查看用戶資訊
報告版大:

我原本是用 PICC 8.05版,後來升級到9.6版後顯示就正常了。

今日又發現有其它的問題,就是呼叫一個 atan 反三角函數運算,但是結果一直不正確,可以呼叫昨日試的 sqrt 開方函數結果是正確的,目前還在嘗試中。

請問 C_H_M 大大,程式要怎麼貼才能像你 PO 的那樣整齊,謝謝。

發表於: 2008/3/21 15:02
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#13
版主
版主


查看用戶資訊
PSIR,
可否麻煩你告知一下所使用的 PICC 版本,Thanks,

C_H_M 大大,
謝謝你的大力協助。


發表於: 2008/3/21 11:25
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#12
資深會員
資深會員


查看用戶資訊
程式可以動了,跟大家報告一下:

原來是我的 Compiler 版本太舊,更新成較新版本之後就可以看到正確的運算結果,連 Watch 視窗的內容也都很清楚。

感謝所有協助小弟的夥伴們。

發表於: 2008/3/20 17:39
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#11
資深會員
資深會員


查看用戶資訊
您好:

我用的 MPLAB 是7.6版,不知道這是否有影響。

請問您執行的結果可以進入 BUF=1; 這一行嗎?

為什麼我的不行?問題到底在哪裡呢?

發表於: 2008/3/20 16:36
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#10
中級會員
中級會員


查看用戶資訊
我把你的程式複製下來試了一下, 得到了正確的結果.
source code 上看起來也應該是正確的.

我能給你的建議只有重新 Build All 一次看看是否會正確了.

發表於: 2008/3/20 16:25
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#9
資深會員
資深會員


查看用戶資訊
C_H_M 您好:
我寫了一個測試程式張貼如下:
CPU用16F917,使用軟體模擬模式,其中 sqrt 函數和上面您貼的一樣。

#include <pic.h> //會使用pic16f91x.h
#include <math.h>

double sqrt_in;
double sqrt_root;
unsigned char BUF;
double sqrt(double x);
/*------------------------------------------------------------------------------*/
// MAIN PROGRAM AREA
/*------------------------------------------------------------------------------*/
void main(void){
sqrt_in = 100;
sqrt_root = sqrt(sqrt_in);
if(sqrt_root == 10){
BUF=1;
}
else{
BUF=0;
}
} //main
//======================================
double sqrt(double x){
double og, ng;
short niter;
int expon;

if(x < 0.0) {
return 0.0;
}
if(x == 0.0) {
return 0.0;
}
og = x;
if(og < 1.0)
og = 1.0/og;
og = frexp(og, &expon);
og = ldexp(og, expon/2);// make an educated guess
if(x < 1.0)
og = 1.0/og;
niter = 20;
do {
ng = (x/og + og)/2.0;
if(ng == og)
break;
og = ng;
} while(--niter);
return og;
}
//======================================

我有在 properties 中開啟屬性視窗,點選 Size 的項目更改為 24 bits(兩個變數均有改),然後當執行 sqrt_in = 100; 這行後,sqrt_in 顯示100.000000 沒有錯,但是當執行下一行時,結果卻不是 10 而是 3.24513602e+032,所以在下面的判斷式中,就無法進入 BUF=1; 這一行,請問要如何做讓計算出來的結果可以符合判斷式。

謝謝。

發表於: 2008/3/20 15:39
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請教C語言如何做開根號運算
#8
中級會員
中級會員


查看用戶資訊
開啟 Watch 監看視窗,

選擇 sqrt_in 項目, 按滑鼠右鍵開啟選單, 點選 properties 開啟屬性視窗.
點選 Size 的項目更改為 24 bits.
相同的, 也更改 sqrt_root 的屬性成為 24 bits.

這樣應該就可以看得到正確的結果值了.

發表於: 2008/3/20 15:12
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------