float

hi everyone. can someone tell me what does mean float..?
There is an example:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
string mystr;
float price=0;
int quantity=0;

cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
Float is floating point value, such as 1.2

http://www.cplusplus.com/doc/tutorial/variables/

good read for such.
price can be a 0.5 for example : its a decimal value.
integers are 1,2,3 type values, if you assign an integer 0.5 it sets it to zero.
floats promote in math like you have here:
price(float)*quantity(integer) = type float (so 0.5*11 = 5.5)

float stands for 'floating point' which is an archaic term for how decimal numbers are implemented in computers.
Last edited on
thanks!
float is a primitive type in C++.
https://www.geeksforgeeks.org/c-data-types/

Floats use floating-point notation/mathematics to represent an approximation of real numbers. e.g. 3.14

Integers (e.g. -1, 42), can only be whole numbers, while floats can be 1.0 or 1.5, etc.

To add to the other posts:
If you dig into the details, there's lots of things to be cautious about with floating-point numbers, such as round-off issues, because almost all real numbers can only be approximately represented by floating-point numbers.
e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <iomanip>
#include <limits>

int main()
{
    float a = 0.1f;
    float b = 0.2f;
    std::cout << a + b << '\n';
    
    int precision = std::numeric_limits<float>::max_digits10;
    std::cout << std::setprecision(precision) << a + b << std::endl;
    
    std::cout << (100000 * 0.3f == 100000 * a + 100000 * b) << '\n';
}

0.3 as a floating-point number isn't exactly 0.3.
Rounding issues cause the equality (100000 * 0.3f == 100000 * a + 100000 * b) to return false.
Last edited on
Topic archived. No new replies allowed.