C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Bug Report

From: Chris Ryan <chrisr98008_at_[hidden]>
Date: Fri, 12 Jul 2024 00:18:33 -0700
That is not the kind of "bug" you would report here. This bug is in
your usage, not C++.

You used pow(...) (see: https://en.cppreference.com/w/cpp/numeric/math/pow)
pow() takes a floating point number and returns a floating point
you then add it to an integer (truncating the floating point value).
It is a well known fact that floating point numbers do not store exact
values,
conversions to & from other types and/or comparisons with floating point
numbers do not work well.


On Thu, Jul 11, 2024 at 11:34 PM Faheem Mohd Parray via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> i was compiling the code and found out this error here is the code and the
> output
> #include <iostream>
> #include <cmath>
> using namespace std;
>
> // armstrong no acb = a^3 + b^3 + c^3
> int main()
> {
> int n;
> cin >> n;
>
> int temp = n;
> int armstrong = 0;
> while (temp > 0)
> {
> int last = temp%10;
> armstrong += pow(last,3);//((temp%10)*(temp%10)*(temp%10))
> cout<< armstrong<<endl;
> temp = temp / 10;
> }
>
> if (armstrong == n)
> {
> cout << "Armstrong No. : " << n << endl;
> }
> else
> {
> cout << "Not an Armstrong No. : " << armstrong << endl;
> }
>
> return 0;
> }
> Output:
> Armstrong_No> .\armstrong.exe
> 153
> 27
> 151 //here should be 125+27 =152 but its 151
> 152 //as the sum must be 153 but Its 152 I tried the other
> as well the worked but not this
> Not an Armstrong No. : 152
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-07-12 07:18:51