Hi
Recently I've made a stupid bug. Here is a snippet:
long long a, b;
....
int c = min(a, b);
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
Alexandru 3 1661
Alexandru Mosoi wrote:
Recently I've made a stupid bug. Here is a snippet:
long long a, b;
...
int c = min(a, b);
Note that C++ does not have 'long long' type.
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
If you need an answer to a G++ specific question, consider posting
to the G++ specific newsgroup, please.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Alexandru Mosoi wrote in message...
Hi
Recently I've made a stupid bug. Here is a snippet:
long long a, b;
...
int c = min(a, b);
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
Alexandru
Why not just check it:
{
long long a, b;
...
if( min(a, b) long long( std::numeric_li mits<int>::max( ) ) ){
throw std::runtime_er ror(" result too large for type int.");
}
int c = min(a, b);
}
Or, truncate it:
int c = ( min(a, b) & 0x7fffffff ); // assuming sizeof int == 4
Check if it was truncated:
if( long long( c ) != min(a, b) ){
std::cout<<"Tru ncated"<<'\n';
}
For compiler questions, you need to ask on an NG for your compiler.
For GCC g++, gnu.g++.help
I think if you just turn on warnings (-Wall -W), it will tell you of
incorrect assignments. ( I've got a big project in progress, or I'd test
that out.)
Also:
-Wlong-long
Warn if long long type is used. This is default. To inhibit the warning
messages, use -Wno-long-long. Flags -Wlong-long and -Wno-long-long are taken
into account only when -pedantic flag is used.
--
Bob R
POVrookie
On Sep 9, 12:08 am, "BobR" <removeBadB...@ worldnet.att.ne twrote:
Alexandru Mosoi wrote in message...
Hi
Recently I've made a stupid bug. Here is a snippet:
long long a, b;
...
int c = min(a, b);
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
Alexandru
Why not just check it:
{
long long a, b;...
if( min(a, b) long long( std::numeric_li mits<int>::max( ) ) ){
throw std::runtime_er ror(" result too large for type int.");
}
int c = min(a, b);
}
Or, truncate it:
int c = ( min(a, b) & 0x7fffffff ); // assuming sizeof int == 4
Check if it was truncated:
if( long long( c ) != min(a, b) ){
std::cout<<"Tru ncated"<<'\n';
}
None of you solution is good for me because I accidentally wrote _int
c_ instead of _long long c_. However I would have avoided the mistake
if compiler was a little bit clever. I've done the bug in a
programming contest where you don't really care about portability and
stuff like that. If it compiles than it perfect. However little
warnings are more than welcome.
>
For compiler questions, you need to ask on an NG for your compiler.
For GCC g++, gnu.g++.help
hmm... i've posted a message, i'm waiting for a reply.
I think if you just turn on warnings (-Wall -W), it will tell you of
incorrect assignments. ( I've got a big project in progress, or I'd test
that out.)
Also:
-Wlong-long
Warn if long long type is used. This is default. To inhibit the warning
messages, use -Wno-long-long. Flags -Wlong-long and -Wno-long-long are taken
into account only when -pedantic flag is used.
>
--
Bob R
POVrookie
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Anton Pervukhin |
last post by:
Hello!
Imagine the situation you have a class that interprets the command line
of the program you are executing. This class is written by third party
and has a main function parse with arguments the same as main:
ClassA::parse(char **argv, int argc)
While trying to write test cases for this function I came across the
warnings appeared when I tried to initialize 'char** argv' straightly
|
by: Steve Richter |
last post by:
here is a warning I am getting in a C++ .NET compile:
c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion;
more than one user-defined conversion has been implicitly applied
while calling the constructor 'MyString::MyString(const
wchar_t *)'
c:\SrNet\jury\JuryTest.h(21) : see declaration of
'MyString::MyString'
The class "StringData" uses a, whatever you call it, operator const
|
by: Josué Andrade Gomes |
last post by:
Take this code:
1: int main()
2: {
3: unsigned short x = { 1, 2, 3, 4, 5 };
4: unsigned short sum = 0;
5:
6: for (int i = 0; i < 5; ++i) {
7: sum += x;
8: }
9: }
|
by: tim |
last post by:
We've started to use a coding standards checker at work. The following
code results in a warning:
38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool
bSaveLast)
39: :CLog()//call base class constructor to initialize members
40: {
41:
42: m_bTimeStamp=bTimeStamp;
43: try
|
by: Gilles Rochefort |
last post by:
Hello,
I wrote the following code to wrap some existing C functions, and I get
some warning about converting float to int.
float zfactor;
int tmp_x, corners_x;
std::transform(tmp_x,tmp_x+4,corners_x, bind2nd( multiplies<float>(),zfactor) );
| |
by: chauhan.alok |
last post by:
hi
I am a beginner to vc++.
I am opening a file("c:\Mydoc.doc") and reading data from it and after
that i just want to write data of 1.2 MB part from first
file("c:\Mydoc.doc") to New file("c:\Newdoc1.doc") and remaining to
other file("c;\Newdoc2.doc"). Code is Giving warning .
Pls Help me How these waring can be removed?
Code is:
|
by: santosh |
last post by:
Hello all,
Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?
|
by: Roman Mashak |
last post by:
Hello,
this piece of code, compiled with gcc-3.4.4 (-ansi -pedantic -W -Wall),
doesn't generate any warnings regarding conversion from 'double' to
'unsigned int'. Is it normal or a bug of compiler?
#include <stdlib.h>
#include <stdio.h>
void f1(unsigned int j)
|
by: d major |
last post by:
I was very puzzled about the conversion between float and long, I
cann't understand why a long val can convert to a float, as the below
codes show:
typedef unsigned long u_long;
float val = 3.14159;
u_long nw_val = *((u_long *) &val);
than the nw_val equal to 1078530000, I made such conversion:
float d_val = *((float*)&nw_val);
|
by: Eric |
last post by:
I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:
char *aVar = "aString";
or
void aFunc( char *aVar) { ... }
aFunc( "aString" );
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |