Jz-c-11

Source: Internet
Author: User

Sword Point of Offer question 11th: the integer number of values

1 //============================================================================2 //Name:jz-c-11.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description: integer number of values7 //============================================================================8 9#include <iostream>Ten using namespacestd; One  A BOOLEqualDoubleNUM1,Doublenum2); - DoublePower (Double Base,intexponent) { -     Doubleresult =1; the     if(Exponent >0) { -         if(Equal (Base,0.0)) { -result =0; -}Else { +              while(Exponent >0) { -result = result *Base; +exponent--; A             } at         } -}Else if(Exponent = =0) { -         if(Equal (Base,0.0)) {//here can not directly judge exponent = = 0, the reason: The computer represents a decimal error, judge whether two decimals is equal, can only judge the absolute value of their difference is not in a very small range, such as less than 0.0000001★★ -cout <<"no meaning"<<Endl; -result =0;//0 of the 0-time party doesn't make sense ★ -}Else { inresult =1; -         } to}Else { +         if(Equal (Base,0.0)) { -result =0; the}Else { *exponent =-exponent;//take absolute value $              while(Exponent >0) {Panax Notoginsengresult = result *Base; -exponent--; the             } +result =1/result; A         } the  +     } -     returnresult; $ } $ BOOLEqualDoubleNUM1,DoubleNUM2) {//determine whether two decimals are equal ★ -     if((Num1-num2 >-0.0000001) && (Num1-num2 <0.0000001)) { -         return true; the}Else { -         return false;Wuyi     } the } - /* Wu * This function is not used here, the advantage of this function is that the efficiency of bit operation is higher than that of multiplication method and redundancy operation. -  */ About /*Double powerwithunsignedexponent (double base, unsigned int exponent) { $ if (exponent = = 0) { - return 1; -     } - if (exponent = = 1) { A return base; +     } the double result = powerwithunsignedexponent (base, exponent >> 1);//Shift right instead of divided by 2★ - result *= result; $ if ((exponent & 0x1) = = 1) {//use and operation to determine the exponent is odd or even ★ the result *= base; the     } the return result; the }*/ -  in intMain () { the     Doubleresult = Power (9,5); thecout << Result <<Endl; About     return 0; the}

Jz-c-11

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.