division by a potentially null global const variable

P

PS

I have a function with a division by a global const variable n defined
in a seperate header file.
How can I handle the case when this variable is null ?
Patrick

// myconstants.h
double const N = 100.;
double const x = 0.; // 1.0
double const n = x*N;
double const c = 3.9;

// file.cpp
double f(double E) {
double y = 0.;
if (n> 0)
y = 73. + std::log(E/c/n); // line 13
return y;
}

file.cpp: In function `double f(double)':
file.cpp:13: warning: division by zero in ``rdiv_expr' not supported by
dump_expr
file.cpp:13: sorry, unimplemented: <expression error> / 0.'
 
M

Mike Wahler

PS said:
I have a function with a division by a global const variable n defined in a
seperate header file.
How can I handle the case when this variable is null ?

if(y != 0)
z = x / y;
else
// can't divide by zero

-Mike
 
M

Marcus Kwok

Mike Wahler said:
if(y != 0)
z = x / y;
else
// can't divide by zero

I agree, however, in the OP's code there is:

if (n> 0)
y = 73. + std::log(E/c/n); // line 13

so I don't understand why it is failing.
 
M

mlimber

Marcus said:
I agree, however, in the OP's code there is:

if (n> 0)
y = 73. + std::log(E/c/n); // line 13

so I don't understand why it is failing.

Perhaps there is a constant (or #define) c that is hiding the global?
With such simple names, it's not hard to imagine.

If that previous snippet doesn't qualify, can the OP post a *complete*
sample that we can copy and paste that demonstrates the problem? What
compiler and version generates those messages?

Cheers! --M
 
P

PS

Perhaps there is a constant (or #define) c that is hiding the global?
With such simple names, it's not hard to imagine.

If that previous snippet doesn't qualify, can the OP post a *complete*
sample that we can copy and paste that demonstrates the problem? What
compiler and version generates those messages?

Cheers! --M
Hi,
Below is a compilable example of my problem.
It was compiled with g++ 3.3.1.
Thanks for your answers,
Patrick

// file.cpp
double const y = 0;

int main(int argc, char *argv[]) {

double x = 1, z=0;

if(y != 0)
z = x / y; // line 8

return 0;
}

// compiler message
$ g++ -c file.cpp
file.cpp: In function `int main(int, char**)':
file.cpp:8: warning: division by zero in `x / 0.'
 
P

PS

PS a écrit :
// file.cpp
double const y = 0;

int main(int argc, char *argv[]) {

double x = 1, z=0;

if(y != 0)
z = x / y; // line 8

return 0;
}

Of course, it compiles fine without 'const', but if possible I would
rather avoid using non-const global variables.
P.
 
M

Marcus Kwok

PS said:
PS a ?crit :
// file.cpp
double const y = 0;

int main(int argc, char *argv[]) {

double x = 1, z=0;

if(y != 0)
z = x / y; // line 8

return 0;
}

Of course, it compiles fine without 'const', but if possible I would
rather avoid using non-const global variables.

Hmm, your example compiles with no errors or warnings with VS .NET 2003.
Maybe since y is const, gcc just replaces all occurrences of y with a
literal 0 value, thus triggering the warning (not error).

I would suggest trying a gcc list or newsgroup.
 
T

TJW

Hello,

PS said:
PS a écrit :
// file.cpp
double const y = 0;
int main(int argc, char *argv[]) {
double x = 1, z=0;
if(y != 0)
z = x / y; // line 8
return 0;
}

Of course, it compiles fine without 'const', but if possible I would rather
avoid using non-const global variables.
P.

Compiles without complaint on gcc 4.0.0 (with and without the
constant declaration) ....


Good Luck,
TJW
 
M

Michiel.Salters

PS said:
I have a function with a division by a global const variable n defined
in a seperate header file.
How can I handle the case when this variable is null ?
Patrick

// myconstants.h
double const N = 100.;
double const x = 0.; // 1.0
double const n = x*N;
double const c = 3.9;

// file.cpp
double f(double E) {
double y = 0.;
if (n> 0)
y = 73. + std::log(E/c/n); // line 13
return y;
}

The ?: operator works for constants, so you can write
y = 73 + std::log(E/c/(n>0?n:1));

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top