I'm trying to display the answer float value as it is, but visual studio code is rounding the float number or displaying the answer as standard number.(i.e:2.5e+07) What should I do? I tried to convert the answer to string, but the answer was rounded.
Here's an example input:
46 615683844
431749087 271781274 274974690 324606253 480870261 401650581 13285442 478090364 266585394 425024433 588791449 492057200 391293435 563090494 317950 173675329 473068378 356306865 311731938 192959832 321180686 141984626 578985584 512026637 175885185 590844074 47103801 212211134 330150 509886963 565955809 315640375 612907074 500474373 524310737 568681652 315339618 478782781 518873818 271322031 74600969 539099112 85129347 222068995 106014720 77282307
The output in theory should be '22258199.5' but visual studio code displays it as: 2.22582e+007
to cout << fixed << ans;
When I tested it that gave an output
22258200.000000
You should be using double, not float, here, given the number of significant figures in your input.
However, when I change all occurences of float to double I get an answer of 0.0. So, what is your code supposed to do, and why do you suppose the answer is what it is?
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
size_t n {}, s {};
cin >> n >> s;
vector<double> l, rad;
double temp {};
for (size_t i = 0; i < n; ++i) {
cin >> temp;
l.push_back(temp);
}
sort(l.begin(), l.end());
if (l[l.size() - 1] != s) {
l.push_back(s + s - l[l.size() - 1]);
rad.push_back(s - l[l.size() - 1]);
}
if (l[0] != 0.0) {
l.push_back(0.0 - l[0]);
rad.push_back(l[0]);
}
sort(l.begin(), l.end());
for (size_t i = 2; i < l.size(); ++i)
rad.push_back((l[i] - l[i - 1]) / 2);
sort(rad.begin(), rad.end());
double ans {};
for (size_t i = 0; i < rad.size(); ++i)
for (size_t j = 1; j <= l.size(); ++j)
if (l[j - 1] + rad[i] < l[j] - rad[i]) {
ans = rad[i + 1];
j = l.size();
}
cout << fixed << setprecision(2);
cout << ans << '\n';
}
which displays
22258199.50
for the given inputs.
Note that:
if (l[0] != 0.0) {
may not always work as expected. The way floating point numbers are held, an expected 0 may not actually be stored as exactly 0. Usually an equality is done something like:
if (std::abs(l[0]) < 0.000005)
where 0.000005 is the required epsilon value which is less than the used accuracy.
also, the number is not 'rounded', the display of the number is! What you see and what the internal value actually is may be very different depending on how you choose to display it, and this is a very important thing to keep in mind, moreso when you don't want to see all the digits but need them when doing computations.
@Tsenguun,
BTW, your line 40 invariably accesses array l[] out of bounds and your line 42 potentially accesses array rad[] out of bounds ... so any "answer" to whatever you are doing is likely to be highly unpredictable.
@lastchance,
I'm trying to solve a problem from codeforces. Now that I think of it, I probably should've posted this question on the beginner coding section. Anyways, here's the problem(link): https://codeforces.com/problemset/problem/492/B
@seeplus,
I tried your code out but Microsoft visual studio code can't recognize all of the declaration and displays:
Test.cpp:10:10: error: expected ';' at end of declaration
size_t n {}, s {};
^
;
Test.cpp:12:14: error: use of undeclared identifier 's'
cin >> n >> s;
^
Test.cpp:15:13: error: expected ';' at end of declaration
double temp {};
^
;
Test.cpp:24:25: error: use of undeclared identifier 's'
if (l[l.size() - 1] != s) {
^
Test.cpp:25:15: error: use of undeclared identifier 's'
l.push_back(s + s - l[l.size() - 1]);
^
Test.cpp:25:19: error: use of undeclared identifier 's'
l.push_back(s + s - l[l.size() - 1]);
^
Test.cpp:26:17: error: use of undeclared identifier 's'
rad.push_back(s - l[l.size() - 1]);
^
Test.cpp:41:12: error: expected ';' at end of declaration
double ans {};
^
;
is there any other compiler for c++ other than visual studio for Mac you could suggest me?
@Tsenguun - The posted code compiles OK with VS2019. What version of the compiler are you using? You also need to set the C++ Language Standard to C++17.