• 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語言如何做開根號運算
#7
資深會員
資深會員


查看用戶資訊
原來如此,請問資料型態要如轉換讓 Watch 視窗中在執行第一行程式時,sqrt_in 的內容顯示 6.25,而執行第二行程式時,sqrt_root 的內容顯示 2.5(√6.25=2.5)。

謝謝。

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


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


查看用戶資訊
監看時資料錯誤嗎!

可能是監看的資料型態選擇不正確.
PICC double 的資料型態與一般常用的 32 bits double型態是不同的,
它預設為 24 bits 的資料型態.
所以你可能需注意一下監看時的資料型態選擇是否正確.

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


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


查看用戶資訊
多謝 C_H_M 的指導:

我有照上述方法,加 sqrt.c 的內容直接加在程式中,並且可以組譯,
但是當在執行這兩行程式時:

sqrt_in = 6.25;
sqrt_root = sqrt(sqrt_in);

從 Watch 視窗看到的結果卻很奇怪,例如執行 sqrt_in = 6.25; 這行後,sqrt_in 變數的內容為:5.949218e-039,再執行下一行後,sqrt_root 的內容為:1.079067e-038。

Watch 視窗中的資料顯示格式是 IEEE Float ,請問資料要怎麼轉換才可以顯示正常的10進制方式。

謝謝。

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


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


查看用戶資訊
在主程式中加入 #include <math.h>,
然後把 sqrt.c 加入到你的專案中一起編譯與 link 應該就可以了.

不然也可以把 sqrt.c 的內容如以下的方式直接加入你的程式中.

[color=CC0000]// sqrt.c 中使用到的兩個 include 述敘[/color]
#include <math.h>
#include <errno.h>

double sqrt_in;
double sqrt_root;

[
color=CC0000]// 由 sqrt.c 複製來的 開平方函數[/color]
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;
}

void main()
{
 
sqrt_in 6.25;
 
sqrt_root sqrt(sqrt_in);
}


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


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


查看用戶資訊
版主您好:

我是用 PICC ,目前我找到 Hi-tech 有數學函數庫可以用,在安裝目錄下有一個叫 sources 的目錄,裡面都是函數庫所用到的數學函數,其中有一個叫做 sqrt.C ,應該就是開平方的運算程式,但我看不懂這個函數要怎麼使用,例如我想傳入一個值進去運算,然後讓它把結果回傳的話,這行程式要怎麼寫?

謝謝。

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


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


查看用戶資訊
在 C18 的 Math.lib 裡有支援開更號的函數 SQRT( ),可以參考 C:\mcc18\doc\HlpC18Lib.chm 的說明。
底下為其摘錄,參考看看:

sqrt
Function: Compute the square root.
Include: math.h

Prototype: float sqrt( float x );

Remarks: A domain error occurs if the argument x is strictly negative. The principal square root exists and is computable for every non-negative floating point number x.
Return Value: The square root of x.
File Name: sqrt.c



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


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


查看用戶資訊
版主您好:

假設有一個運算式如下:

R=√100

請問C語言要怎麼寫可以算出R=10,謝謝。

發表於: 2008/3/19 14:35
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... ]

教育訓練中心

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