C language-' ^ ': illegal, right operand have type ' double '

Source: Internet
Author: User
Tags pow
Error code
int pi () {double p=0,j=0;double pi=0; P =1/(4*j-1); for (int i=0;i<100000;i++) {pi=-( -1) ^j*p+pi;printf ("%lf", 4*pi); } return 0;} void Main () {//Piint pi ();
}

Wrong reason: pi=-( -1) ^j*p+pi; The mathematical expression cannot be processed directly in C, and the math function needs to be called, and the C-language thinking may not be fully accepted.

Correct code
void Pi2 () { double p=0,j=1; int i; for (i=1;i<10000;i++) //Here I is the number of items { J=pow ( -1.0,i+1)/(2*i-1); The POW (x, y) is used to calculate the y power of x p+=j; printf ("%lf\n", 4*p); Output the value of each item } printf ("%lf\n", 4*p); Output final PI value //return 0;} void Main () {//Seek pi pi2 ();}

  

C language-' ^ ': illegal, right operand have type ' double '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.