You are here:

C++/c related question

Advertisement


Question
write a function to return value of (a+b)2 given a and b are real numbers
please sir, help me to find the answer

Answer
A real number is an abstract mathematical concept; a real number is a value that represents a quantity along a continuum, such as 5 (an integer), 3/4 (a rational number that is not an integer), and pi (an irrational number). A real number can be represented by an infinite decimal representation, such as 3.14159265358979323846264338327950288419716939937510..., where the digits continue indefinitely. The real numbers are sometimes thought of as points on an infinitely long number line.

Real numbers have several mathematical properties. For instance they have no lower or upper bound, they range from minus infinity to plus infinity. They have infinite density - there are an infinite number of real numbers lying between any two real numbers.

Computer representation of numbers, which is by definition a finite representation, can not satisfy these mathematical properties; computers need a fixed size representation of real numbers, on which arithmetic operations can be efficiently performed.

When a fixed size representation is used, say N bits long, we have at most  2^N different bit-patterns, and at most 2^N numbers can be accurately represented. We thus have a finite bounded set of numbers; in every bounded segment, there are infinitely many real numbers, but we have at most 2^N available bit-patterns, so many real numbers will have to be represented by one bit-pattern. One bit-pattern can't represent many numbers equally well, it will represent one of them exactly and the others will be (slightly) misrepresented. These real numbers that can be represented exactly are called floating-point numbers. See http://en.wikipedia.org/wiki/Floating_point

C++ has numeric types which are floating-point numbers; the default floating-point type is double. So you could write a function which takes two parameters of type double as input and returns a double as the result.

   double my_function( double a, double b ) ;  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.