473,545 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cast from function call of type int to non-matching type double

Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 18 '06 #1
16 11223
Martin Jørgensen <ho**********@h otmail.com> writes:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The
line should create a (type double) random number between 0 and 1 and
it would be nice to have this warning go away...


This warning should show up on gcc only when a specific compilation
option is set (I don't remember it, something with -W...). It tells
you if you cast a function call to a type that's different, like
double and int. I think it protects against int malloc() [it's void *
malloc() in the proper header is included]
being cast to a pointer.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jun 18 '06 #2
Martin Jørgensen wrote:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...


gcc has an option, -Wbad-function-cast, that will warn "whenever a
function call is cast to a non-matching type". Either don't use this
option, explicitly disable is using -Wno-bad-function-cast, or better
yet, lose the superfluous cast:

xdouble = rand()/(double)RAND_MA X;

Robert Gamble

Jun 18 '06 #3
Robert Gamble wrote:
Martin Jørgensen wrote:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...

gcc has an option, -Wbad-function-cast, that will warn "whenever a
function call is cast to a non-matching type". Either don't use this
option, explicitly disable is using -Wno-bad-function-cast, or better


Bad idea. I want it to catch "real errors", but not those intentionally
casts I make.
yet, lose the superfluous cast:

xdouble = rand()/(double)RAND_MA X;


Great (problem solved)! I just don't understand: I won't complain if I
divided two numbers of type double and store the result as a type double
-> therefore the cast. So, rand() returns an integer, which is divided
by a type double. This doesn't give any warning....?

What exactly is a "non-matching type"?
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 18 '06 #4
Martin Jørgensen <ho**********@h otmail.com> writes:
Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The
line should create a (type double) random number between 0 and 1 and
it would be nice to have this warning go away...


There should be nothing wrong with that line; casting an int to double
is perfectly legitimate. I don't get a warning when I compile it
myself. Show us a complete program and the *exact* warning message.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #5
Martin Jørgensen said:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;


Why are you casting in the first place? Why aren't you doing this:

xdouble = rand() / (RAND_MAX + 1.0);

instead?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #6
Richard Heathfield wrote:
Martin Jørgensen said:

Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;

Why are you casting in the first place? Why aren't you doing this:

xdouble = rand() / (RAND_MAX + 1.0);


I also do that now, as that don't give any warnings. But I did it
because I thought that I wouldn't get any problem if I divided two type
double-numbers/variables with each other.

So: My idea was: It must be better do do this:

500.0 / 100000.0

Than this:

500 / 100000.0

But the compiler didn't share that logic with me :-)
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 18 '06 #7
On Sun, 18 Jun 2006 07:52:00 +0200, Martin Jørgensen
<ho**********@h otmail.com> wrote in comp.lang.c:
Robert Gamble wrote:
Martin Jørgensen wrote:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...

gcc has an option, -Wbad-function-cast, that will warn "whenever a
function call is cast to a non-matching type". Either don't use this
option, explicitly disable is using -Wno-bad-function-cast, or better


Bad idea. I want it to catch "real errors", but not those intentionally
casts I make.
yet, lose the superfluous cast:

xdouble = rand()/(double)RAND_MA X;


Great (problem solved)! I just don't understand: I won't complain if I
divided two numbers of type double and store the result as a type double
-> therefore the cast. So, rand() returns an integer, which is divided
by a type double. This doesn't give any warning....?


You cannot divide an int by a double in C. The language does not
allow it. When you think you are dividing (or adding, subtracting,
multiplying, and so on) two different arithmetic types, something else
is really happening.

C has conversions of one type to another. Some conversions are
automatic, and will happen automatically if the expression calls for
it. The most common of these are what are called "the usual integer
conversions" in the C standard.

When an expression or subexpression performs a binary operation on two
scalar values of different type, the lesser type is automatically
promoted to the greater type. No cast is required.

So if you have code like this:

double two_thirds;
two_thirds = 2 / 3.0;

....the integer constant '2' is automatically converted to type double
because the divisor is the double constant '3.0'.

Exactly the same thing happens in the expression:

rand() / (double)RAND_MA X;

Both operands are evaluated. The cast on the denominator makes it a
double value. This causes the compiler to automatically convert the
int returned by rand() to a double to do the division.
What exactly is a "non-matching type"?
Well, the type of the return value of rand() is an int. Any type
other than int would be a non-matching type.
Best regards
Martin Jørgensen


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 18 '06 #8
On Sun, 18 Jun 2006 07:52:00 +0200, Martin Jørgensen
<ho**********@h otmail.com> wrote:
Robert Gamble wrote:
Martin Jørgensen wrote:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don't see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...

gcc has an option, -Wbad-function-cast, that will warn "whenever a
function call is cast to a non-matching type". Either don't use this
option, explicitly disable is using -Wno-bad-function-cast, or better


Bad idea. I want it to catch "real errors", but not those intentionally
casts I make.
yet, lose the superfluous cast:

xdouble = rand()/(double)RAND_MA X;


Great (problem solved)! I just don't understand: I won't complain if I
divided two numbers of type double and store the result as a type double
-> therefore the cast. So, rand() returns an integer, which is divided
by a type double. This doesn't give any warning....?


The integer rand returned is first implicitly converted to double and
the division is performed using two doubles. You are not casting the
return from rand to double. Since an implicit conversion is not a
cast, the bad-function-cast option should not be involved.

Note that the compiler is free to generate any kind of diagnostic it
likes as long as it still generates the correct code for a well
defined expression. It could still generate a warning of the type
"Hey, you are dividing an int by a double and I consider that to be an
operation of dubious value." It's a quality of implementation issue.

What exactly is a "non-matching type"?


Whatever your compiler decides it is.
Remove del for email
Jun 18 '06 #9
On Sun, 18 Jun 2006 07:14:02 +0000, Richard Heathfield
<in*****@invali d.invalid> wrote:
Martin Jørgensen said:
Hi,

Short question:

Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MA X;


Why are you casting in the first place? Why aren't you doing this:

xdouble = rand() / (RAND_MAX + 1.0);

instead?


Possibly because it gives a different answer.
Remove del for email
Jun 18 '06 #10

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

Similar topics

5
3730
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const...
5
2357
by: trying_to_learn | last post by:
Hello All Have another qn from the practice exercises. I ve done the exercise and the birds fly member fn was called successfully. But i still cant answer the authors qn. why this ability to assign void * has had to be avoided in C++. I didnt even instantiate bird obj then how was i able to call a bird member fn. Im confused. Quote:...
6
2533
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class called CUtils with a static method called print2std()
5
2994
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
10
10010
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return value of float is changed to float seems not to work; I belive it was like this: typedef int (*foo) (int); function_ptr_bar =...
21
2540
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
11
3401
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
1
2027
by: dearprasan | last post by:
Hi, I am making a call to a asynchronous call to a webservice using callback. Before I make the asynchronous call, I am allowed to make the following cast: IHTMLDocument2 doc = (IHTMLDocument2) this.WebBrowser1.Document.Domdocument; But after the asynchronous call is made, and its callback procedure is handled, i get "Specified cast not...
9
2045
by: jason.cipriani | last post by:
All right, I'm in this weird situation that's hard to explain but I've put together a small example program that represents it. Please bear with the fact that some of the stuff in the example seems useless, it's from a much more complex situation. The example program is confusing for me to think about and to look at, and even though I'm...
3
2076
by: Bartc | last post by:
I have a variable like this: int *p; Sometimes this points to an int location containing the address of a void(void) function. How can I cast it so that I can call that function? I've tried casting to void **(void). The compiler keeps saying such a cast
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
Oralloy
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. ...
1
7436
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...
0
5981
agi2029
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...
0
4958
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...
0
3463
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...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
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
0
715
bsmnconsultancy
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...

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.