what does the .f in 1000.f mean? c++ [duplicate] - c++

This question already has answers here:
Purpose of a ".f" appended to a number?
(5 answers)
Closed 5 years ago.
I am following a tutorial on how to make a game with SDL. At a certain point in the tutorial I need to calculate the game's FPS. The tutorial does the following:
caption << "Average Frames Per Second: " << frame / ( fps.get_ticks() / 1000.f );
Now, I know exactly what the code does, except for the part where it divides by 1000.f. I've been looking but just can't find what .f means.
So my question is, what does .f mean? And why is it there?

1000 is an int literal.
1000. is a double literal.
1000.f is a float literal.

It means this is a float constant rather than a double constant. As for the effect, on most C++ compilers, it will have no effect as the float will be converted to a double to do the divide.

.f makes the number float type.
Just see this interesting demonstration:
float a = 3.2;
if ( a == 3.2 )
cout << "a is equal to 3.2"<<endl;
else
cout << "a is not equal to 3.2"<<endl;
float b = 3.2f;
if ( b == 3.2f )
cout << "b is equal to 3.2f"<<endl;
else
cout << "b is not equal to 3.2f"<<endl;
Output:
a is not equal to 3.2
b is equal to 3.2f
Do experiment here at ideone: http://www.ideone.com/WS1az
Try changing the type of the variable a from float to double, see the result again!

It is telling you the literal 1000. should be treated as a float. Look here for details.

It means 1000.f is treated as a float.

A floating point literal can have a suffix of (f, F, l or L). "f and F" specify a float constant and "l and L" a double.

Related

double math and boolean check gives erroneous answer [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Take this c++ code:
double d = 0.3028 + 0.0028;
cout << d << endl;
if (d == 0.3056)
cout << "match" << endl;
else
cout << "not a match" << endl;
Why is the output "not a match"?
Well that is because how floats are stored in memory. Here is a good article on this: https://dev.to/visheshpatel/how-floating-point-no-is-stored-memory-47od
Instead floats (and doubles) should be checked if are "almost equal". In your case, if you are interested only in 4 decimal places then you can check if the difference is lower than 0.00001. So:
float epsilon = 0.00001;
double a = d; //your value
double b = 0.3056; //the value to which you are comparing
bool equal_ab = abs(a - b) < epsilon;
This is the nature of finite precision math.
If you use, say, six digits of decimal precision, 1/3 will be represented as 0.333333 and if you do "3 * 1/3" you will get 0.999999, not 1.
Similarly, 2/3 will be 0.666667, so 2 * 1/3 will not give 2/3. 1/3+1/3 will give 0.666666, not 2/3.
Finite precision representations are funny this way and testing them for precise equality is generally a bad idea.

write large double number into txt file C++ [duplicate]

This question already has answers here:
How to make C++ cout not use scientific notation
(8 answers)
Closed 3 years ago.
I try to write a large double number which is the time that takes my function to produce the result into txt file( using c++).
for exemple in my console, the function take 0.000029 (time unit), when i write the value into my txt file it is converted into : 2.9e-05
My question: how can i write the value as it is in the console i.e 0.000029 ?
here is my code:
*`
clock_t cPrec1=0;
double duration1 =0.0;
clock_t cTime1;
cTime1 = clock();
bool h= check(4, copy);
duration1 = ( cTime1 - cPrec1 ) / (double) CLOCKS_PER_SEC;
cPrec1 = clock();
outfile << space<< 1 << space << duration1<< space <<'\n' ;
printf(" saved\n");
`
Thanks for helping me.
You can use std::setprecision() and std::fixed function.
when you use a stream you can add a "<< std::setprecision(n)" at the beginning ( or even before the double number ) to set how many number after the dot you want to see. The parameter of this function is an integer that specify the number after the dot that are printed.
Another useful function is std::fixed that can be used like the previous on in the stream ( ex. "<< std::fixed" ). I report to you the definition of the function :
When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
I leave you also 2 useful links for this function:
std::setprecision
std::fixed
Use:
outfile << std::setprecision(7) << duration1

Why does c++ not print out 2/3 as 0.666667? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 3 years ago.
I wrote
double r = 2/3;
cout << "r = " << r << endl;
Then it returns
r = 0
I would like it to show that r = 0.666667 or something like that instead of a 0. Especially I need to use that r = 2/3 to do calculation in my algorithm. How can I fix that?
Because both 2 and 3 are literals of type int, and integer division yields an integer result in C++. What you did is perform integer division first, then promote the integer result to double.
Instead, you want to cast at least one of the operands to float or double (a fractional floating point-type), then the other operand will be promoted to the same type and the resulting value will also be float or double.
Either of these will work:
double r = 2.0/3.0;
double r = 2.0/3;
double r = 2/3.0;

c++ Math Issue, Double not returning value if math is all on the same line

First off thanks for all the great questions and answers on this site! This website has a been a great resource for solving so many of my problems that others have had in the past which have already been answered. However, I've currently got a problem I can't seem to find a solution for..
I'm working with OpenCV trying to make my first C++ program and I'm experiencing a weird math issue. I'm guessing it's something simple that I'm not seeing and I hope someone can explain what I'm doing wrong.
The problem is on the line below:
double L1_Slope = (L1P2.y-L1P1.y) / (L1P2.x-L1P1.x);
it's returning 0 if I do the math all on the same line, but if I break it up into 3 lines, it gives me the output I need.
I've seen similar questions about this, but they all deal with integers.
L1P1,L1P2,L2P1,L2P2 are in fact all cv::Point's - which are integers...
but since I'm declaring L1_Slope as a double, I don't see why this would be the case.
Any Idea's? I know I can break the math up and it will work, but I can't imagine not being able to do this math on a single line.
cv::Point calcIntersection(cv::Point L1P1, cv::Point L1P2, cv::Point L2P1, cv::Point L2P2) {
//calculates the intersection of two lines
std::cout << "\n\nL1P1.x=" << L1P1.x << "\n";
std::cout << "L1P1.y=" << L1P1.y << "\n";
std::cout << "L1P2.x=" << L1P2.x << "\n";
std::cout << "L1P2.y=" << L1P2.y << "\n\n";
double test1 = (L1P2.y - L1P1.y);
double test2 = (L1P2.x - L1P1.x);
double test3 = test1/test2;
std::cout << "test1=" << test1 << "\n";
std::cout << "test2=" << test2 << "\n";
std::cout << "test3=" << test3 << "\n\n";
double L1_Slope = (L1P2.y-L1P1.y) / (L1P2.x-L1P1.x);
std::cout << "L1_Slope=" << L1_Slope << "\n\n";
double L1_Intersect = L1P1.y - L1_Slope * L1P1.x;
double L2_Slope = (L2P2.y - L2P1.y) / (L2P2.x - L2P1.x);
std::cout << "L2_Slope=" << L2_Slope << "\n";
double L2_Intersect = L2P2.y - L2_Slope * L2P2.x;
double intersectionX = (L2_Intersect - L1_Intersect) / (L1_Slope - L2_Slope);
double intersectionY = (L1_Slope * intersectionX + L1_Intersect);
cv::Point intersection = { static_cast<int>(intersectionX), static_cast<int>(intersectionY) };
return intersection;}
Here's what's being output in the console:
L1P1.x=111
L1P1.y=62
L1P2.x=578
L1P2.y=345
test1=283
test2=467
test3=0.605996
L1_Slope=0
L2_Slope=0
double L1_Slope = (L1P2.y-L1P1.y) / (L1P2.x-L1P1.x);
Here, the x and y coordinates are integers. The expression evaluates to:
(345 - 62) / (578 - 111)
or
283 / 467
You'll be surprised to learn that in C++, 283 / 467 is 0. This is integer division, and is carried out as integers, no fractional part. Even though the end result gets assigned to a double, it's too late. The division gets evaluated first. The fractional part was already truncated, so the end result is 0.
double test1 = (L1P2.y - L1P1.y);
double test2 = (L1P2.x - L1P1.x);
double test3 = test1/test2;
Here, you're storing the numerator and the denominator into double variables, separately, then the division divides two double values:
283.0 / 467.0
This is now a floating point division, whose result is .605996
Expressions are evaluated based on their types, not the type of the variable to which their result will be assigned.
When you do the math separately and assign each result to a double variable, you're implicitly casting the sub-expressions to double. So in effect you've got something like
double L1_Slope = static_cast<double>(L1P2.y-L1P1.y) / static_cast<double>(L1P2.x-L1P1.x);
When you put it all into one expression without (implicit or explicit) casts, you're doing integer math, more like
int L1_Slope_int = (L1P2.y-L1P1.y) / (L1P2.x-L1P1.x);
double L1_Slope = L1_Slope_int;
In this case, if the x difference exceeds the y difference, rounding will mean your less-than-one fraction will turn into an integer 0.
To force the math to double, you need to ensure at least one operand of each operation where it matters is a double (the other will be implicitly upcast if necessary). You can accomplish this either by assigning the result of one or more of the subexpressions to a double variable, as you saw, or by explicitly casting to double as shown in the first snippet in this answer.

Loss of precision when using pow in C++

10^1.64605 = 44.2639330165
However in C++ using pow:
double p = pow(10,1.64605) returns 44.2641.
Is there a way to increase the precision here? I tried casting both sides to long double but that didn't help either.
More interesting is:
cout<<p;
double a = -1.64605;
cout<<pow(10,-a);
p = pow(10, (-p));
cout<<p;
the output is:
-1.64605
44.2639
44.2641
Why?
cout is truncating your double for display, but the value calculated by pow is probably at least as precise as you expect. For how to get more precision displayed in the console see:
How do I print a double value with full precision using cout?
I'll elaborate given the prodding by David.
You stated that double p = pow(10,1.64605) returns 44.2641 but this is incorrect. It returns 44.26393301653639156; without any formatting specifiers this displays as 44.2639 (as you see later).
When you cout the original value of p in the second code snippit it displays -1.64605 (due to reduced precision formatting) and you are assuming that it is exactly -1.64605 whereas it is actually somewhere between -1.64605115... and -1.64605213..., which does evaluate to 44.2641 in the expression cout << pow(10, (-p));
The answer will be found by examining p. You did not show how it was initialized.
You will find that p != a, even though they appear to be the same when you print them to the console. When you printed to the console you only printed the first 6 significant decimal digits. Print both values to greater precision and you will see that they are not equal.
You said that:
double p = pow(10,1.64605);
evaluates to 44.2641. But that is not true. If you actually execute that code you will see that.
double p = pow(10,1.64605);
cout << p;
outputs 44.2639.
Your code differs slightly from that above. It is:
cout << p;
p = pow(10, (-p));
cout << p;
Output the original value of p to full precision and all will be revealed.