473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

conversion warning

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

Sep 8 '07 #1
3 1638
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
Sep 8 '07 #2

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_limits<int>::max() ) ){
throw std::runtime_error(" 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<<"Truncated"<<'\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
Sep 8 '07 #3
On Sep 9, 12:08 am, "BobR" <removeBadB...@worldnet.att.netwrote:
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_limits<int>::max() ) ){
throw std::runtime_error(" 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<<"Truncated"<<'\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

Sep 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
3
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...
1
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: }
2
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: ...
6
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; ...
2
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...
11
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,...
8
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...
8
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 =...
9
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(...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.