473,386 Members | 1,654 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,386 software developers and data experts.

Floating point literals

I was suprised to find that my compiler included a floating point
library for the following line of code:

x = y * 100 * 0.25;

I thought that the compiler was supposed to evaluate literals at
compile time so that this line would become:

x = y * 25;

I tried looking this up in the standard, but couldn't find any section
that specifically addressed the evaluation of mathematical expressions
in literals.

So is this the way it always works, or does it depend on the compiler?
Nov 14 '05 #1
6 2011

"Tony G" <an************@hotmail.com> wrote in message
I was suprised to find that my compiler included a floating point
library for the following line of code:

x = y * 100 * 0.25;

I thought that the compiler was supposed to evaluate literals at
compile time so that this line would become:
x = y * 25;


A good compiler ought to resolve constant expressions at compile time.
What it won't do is convert a floating point expression to an integer, so if
you look at the machine code you will see that it probably loads a register
with constant 25 float, then does a floating point multiply with y, which I
presume is an integer.
Nov 14 '05 #2

"Tony G" <an************@hotmail.com> wrote in message
news:aa**************************@posting.google.c om...
I was suprised to find that my compiler included a floating point
library for the following line of code:

x = y * 100 * 0.25;

I thought that the compiler was supposed to evaluate literals at
compile time so that this line would become:

x = y * 25;

I tried looking this up in the standard, but couldn't find any section
that specifically addressed the evaluation of mathematical expressions
in literals.

So is this the way it always works, or does it depend on the compiler?

If that's what you want, write it that way:
x=y*(int)(100*.25);

If you wrote
x=100*.25*y;
You would have a reasonable expectation, with left to right evaluation, of
getting
x=25.*y;
Nov 14 '05 #3
Tim Prince wrote:

"Tony G" <an************@hotmail.com> wrote in message
news:aa**************************@posting.google.c om...
I was suprised to find that my compiler included a floating point
library for the following line of code:

x = y * 100 * 0.25;

I thought that the compiler was supposed to evaluate literals at
compile time so that this line would become:

x = y * 25;

I tried looking this up in the standard,
but couldn't find any section
that specifically addressed the evaluation of mathematical
expressions in literals.

So is this the way it always works,
or does it depend on the compiler?

If that's what you want, write it that way:
x=y*(int)(100*.25);


x = y * (100 * 0.25);
.... should do the trick.

But OP's original statement is equivalent to
x = (y * 100) * 0.25;
and it would be wrong for the compiler to combine
the constants into a constant expression that doesn't exist
in the original statement.

--
pete
Nov 14 '05 #4
I tried several of the suggestions with the following results:

1) x = y * 100 * 0.25; // float lib included
2) x = 100 * 0.25 * y; // float lib included
3) x = y * (100 * 0.25); // float lib included
4) x = y * (int)(100 * 0.25); // float lib NOT included
5) static int x = 100 * 0.25; // float lib NOT included
6) int x = 100 * 0.25; // float lib included

For the first three, apparently the compiler is performing the
multiply at run time because its not recognizing that the result is
going to be converted into an integer (x and y are integers, BTW).
Adding the cast on 4 is enough of a hint for the compiler to catch on
and do the math at compile time. Using the multiply in a static
initializer in 5 forces the compiler to try to fully evaluate the
expression at compile time since the expression must be constant. But
since an initializer for a local doesn't have to be constant, for 6
the compiler performs the multiply at run time.

So at least now I understand what's going on. But I still don't know
what the C standard says about it. I'm pretty sure that 5 will either
work or generate an error message (i.e., "Initializer must be
constant..."). But can I count on 4 always working, or is it
implementation dependant?
Nov 14 '05 #5
Tony G wrote:
But I still don't know what the C standard says about it.
I'm pretty sure that 5 will either
work or generate an error message (i.e., "Initializer must be
constant..."). But can I count on 4 always working, or is it
implementation dependant?


Unspecified.
A program is allowed to evaluate it's constant expressions
at compile time or run time.

--
pete
Nov 14 '05 #6
an************@hotmail.com (Tony G) wrote in message news:<aa**************************@posting.google. com>...
I tried several of the suggestions with the following results:

1) x = y * 100 * 0.25; // float lib included
2) x = 100 * 0.25 * y; // float lib included
3) x = y * (100 * 0.25); // float lib included
4) x = y * (int)(100 * 0.25); // float lib NOT included
5) static int x = 100 * 0.25; // float lib NOT included
6) int x = 100 * 0.25; // float lib included

For the first three, apparently the compiler is performing the
multiply at run time because its not recognizing that the result is
going to be converted into an integer (x and y are integers, BTW).
It is just as likely that the compiler is performing the multiply
at compile time, but not performing the cast at compile time.
In general int(x*a) is not equal to x*int(a) even if x is an integer.
It is a lot to ask of the compiler that is recognize that
even though 25. is a floating point constant its value is
integral. The float lib is still needed to compute
x*25.
Adding the cast on 4 is enough of a hint for the compiler to catch on
and do the math at compile time.
Or possibly allows the cast from 25. to 25 which removes the need
for the float lib.

Using the multiply in a static
initializer in 5 forces the compiler to try to fully evaluate the
expression at compile time since the expression must be constant.
But since an initializer for a local doesn't have to be constant, for 6
the compiler performs the multiply at run time.

The compiler could perform this multiply at run time but it does not
have to. Again you seem to be assuming that if the float lib is loaded
the multiply must be done at run time. This may or may not be true.
So at least now I understand what's going on. But I still don't know
what the C standard says about it. I'm pretty sure that 5 will either
work or generate an error message (i.e., "Initializer must be
constant..."). But can I count on 4 always working, or is it
implementation dependant?


If you mean can you count on 4 leading to x having the value 25*y, the answer
is yes. If you mean can you count on 4 forcing the evaluation of (100 * 0.25)
at compile time the answer is no, the compiler may evaluate
this at compile time but does not have to. If you mean can you count on 5
leading to the float lib not being loaded the answer is no. The standard
is silent on when the float lib should be loaded, or indeed if there
is such a beast as a float lib in the first place. The implementation
could choose to load the float lib whenever there is a full moon.
More likely in practice, the implementation may use heuristics to
decide when to load float lib. Since not loading float lib when
needed is a far more serious error than loading float lib when not
needed the heuristics will be chosen to favour the latter.

-William Hughes
Nov 14 '05 #7

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

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
10
by: Peter Ammon | last post by:
I'm fuzzy on exactly what I can depend on when doing floating point comparisons. I know that calculated values can differ, e.g. I'm not guaranteed that 1. + 2. == 3. What about comparisons with...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
4
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating...
6
by: Cezary Noweta | last post by:
Hello, In Control Panel, Regional Settings, set decimal point to something other then dot ,,.'' (ASCII 0x2E). Let it be comma (ASCII 0x2C) for example. Compile and run the following code ===...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.