Hi all
I am writing a small app that uses real numbers all over the place for
calculations.
But when it comes to displaying values it is better to only display an it,
(especially when it comes to % values and so on).
so I first thought that the best way would be...
....
double x = 1.23456789012345;
int y = (int)x;
but that does not always work, in fact when the double has a lot of decimal
places I get a rather nasty looking number.
I am assuming that the casting could get it wrong if there are too many
decimal places.
so I thought I'd use the ceil(...); and floor(...); functions
....
y = (x>=0)?(int((floor(x))) : (int(ceil(x))));
the logic been that the decimal places are removed by both functions and the
casting then works just fine.
but that raises a few questions.
Why does the cast not work some times? is the floor/ceil method "guaranteed"
to work? What would be the preferred/best method?
Many thanks in advance.
Simon 6 7309
"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2p************@uni-berlin.de... Hi all
I am writing a small app that uses real numbers all over the place for calculations.
But when it comes to displaying values it is better to only display an it, (especially when it comes to % values and so on).
so I first thought that the best way would be... ...
double x = 1.23456789012345; int y = (int)x;
but that does not always work, in fact when the double has a lot of decimal places I get a rather nasty looking number. I am assuming that the casting could get it wrong if there are too many decimal places.
so I thought I'd use the ceil(...); and floor(...); functions ... y = (x>=0)?(int((floor(x))) : (int(ceil(x))));
the logic been that the decimal places are removed by both functions and the casting then works just fine.
but that raises a few questions.
Why does the cast not work some times? is the floor/ceil method "guaranteed" to work? What would be the preferred/best method?
The C++ Standard, 4.9[1] says that a rvalue of a floating point type
can be converted
to a rvalue of an integral type by truncating (discarding the
fractional part). However,
as expected, if the truncated value cannot be represented in the
integral type, the
behavior is undefined.
Are you getting abnormal results when using very large double values ?
Also, why are you
using C-style casts ? It is preferred to use the C++ casts:
double x = 1.234;
int y = static_cast<int>(x);
Have you tested the code with other compilers ?
Vladimir Ciobanu
"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2p************@uni-berlin.de... I am writing a small app that uses real numbers all over the place for calculations.
But when it comes to displaying values it is better to only display an it, (especially when it comes to % values and so on).
so I first thought that the best way would be... ...
double x = 1.23456789012345; int y = (int)x;
but that does not always work, in fact when the double has a lot of decimal places I get a rather nasty looking number.
Could you provide actual examples of these "nasty looking" numbers ?
One thing that might be happening is that the number is too large
to be stored in an int...
I am assuming that the casting could get it wrong if there are too many decimal places.
so I thought I'd use the ceil(...); and floor(...); functions ... y = (x>=0)?(int((floor(x))) : (int(ceil(x))));
the logic been that the decimal places are removed by both functions and the casting then works just fine.
but that raises a few questions.
Why does the cast not work some times? is the floor/ceil method "guaranteed" to work? What would be the preferred/best method?
floor/ceil will work, but if the number is beyond the range of int,
the same problem will occur when the conversion is made.
This said, if you are only performing these conversions for
display purposes, it may be possible to specify the format for
the output of the double value instead of converting to an int.
Consider for example:
using namespace std; //after include <iostream> <iomanip> <cstdio>
double d = 12.3456;
cout << setprecision(0) << setiosflags(ios_base::fixed) << d << endl;
printf("%.0f\n",d); // if you still prefer c-style output
hth,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2p************@uni-berlin.de... Hi all
I am writing a small app that uses real numbers all over the place for calculations.
But when it comes to displaying values it is better to only display an it, (especially when it comes to % values and so on).
so I first thought that the best way would be... ...
double x = 1.23456789012345; int y = (int)x;
but that does not always work, in fact when the double has a lot of
decimal places I get a rather nasty looking number.
1. How do you know it isn't working?
2. It works fine with g++
3. Post a complete, small program that shows the symptom (example below) and
we'll look at it further.
#include <iostream>
int main( )
{
double x = 1.23456789012345;
int y = (int)x;
std::cout << x << ":" << y << std::endl;
return 0;
}
--
Gary
> 1. How do you know it isn't working?
I could see it. :).
2. It works fine with g++
I am using VC6++, on windows XP. (I know everybody has strong feeling about
MS products but that's not the point here I hope).
3. Post a complete, small program that shows the symptom (example below) and we'll look at it further.
Well, it seems to work fine on my XP pro machine.
Before I have to totally bury my head in shame, I will try and look at more
windows machine. The problem was reported on a Win98SE machine.
I guess I'll also need to look if I am not trying to work with doubles that
are out of int range.
Regards
Simon
> The C++ Standard, 4.9[1] says that a rvalue of a floating point type can be converted to a rvalue of an integral type by truncating (discarding the fractional part). However, as expected, if the truncated value cannot be represented in the integral type, the behavior is undefined.
Sorry, can I be rude and ask to see that section of the standard?
Simon
"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2p************@uni-berlin.de... The C++ Standard, 4.9[1] says that a rvalue of a floating point type can be converted to a rvalue of an integral type by truncating (discarding the fractional part). However, as expected, if the truncated value cannot be represented in the integral type, the behavior is undefined.
Sorry, can I be rude and ask to see that section of the standard?
Yes. Here it is:
4.9[1]: "An rvalue of a floating point type can be converted to an
rvalue of an integer type. The conversion truncates; that is, the
fractional part is discarded. The behavior is undefined if the
truncated value cannot be represented in the destination type."
Vladimir Ciobanu This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Hunter |
last post by:
I am using pycxx 5.2.2 to generate some extension code. I want to
extract some doubles from some python sequences
When I do
double l( Py::Float(rect) );
double b( Py::Float(rect) );
and...
|
by: Mikheil |
last post by:
Hello!
I need to translate file destination name with one backslashes
"c:\program files\directory\file.txt"
to string containing double backslashes
"c:\\program files\\directory\\file.txt"
If...
|
by: Christian Meier |
last post by:
Hallo NG
My problem is the deviation of the floating point datatypes. 90.625 is not
exactly 90.625. It is 90.624999999.... on my system. Now I tried to find a
solution for my problem but I...
|
by: DAVID SCHULMAN |
last post by:
I've been trying to perform a calculation that has been running into
an underflow (insufficient precision) problem in Microsoft Excel, which
calculates using at most 15 significant digits. For this...
|
by: Bjørn Augestad |
last post by:
Below is a program which converts a double to an integer in two
different ways, giving me two different values for the int. The basic
expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
|
by: Web learner |
last post by:
In the following code, I want ArrayList object x to be of type double.
How can I do that?
--Thanks
SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS;...
|
by: Web learner |
last post by:
The following code works fine
private List<double> GetDataFor(string column, int selectedYear) {
-------
-------
return list;
}
foreach (double item in GetDataFor("AirTemp", selectedYear))...
|
by: NeoPa |
last post by:
Background
Whenever code is used there must be a way to differentiate the actual code (which should be interpreted directly) with literal strings which should be interpreted as data. Numbers don't...
|
by: =?Utf-8?B?cGF0cmlja2RyZA==?= |
last post by:
Hi everyone!
I'm using greece - greek in my control panel's regional options,
and so, my decimal point is the comma (,),
while it is the dot (.) for the sql server db,
however, I'm facing...
|
by: Bill Reid |
last post by:
I just noticed that my "improved" version of sscanf() doesn't assign
floating point numbers properly if the variable assigned to is declared
as a "float" rather than a "double". (This never...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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: 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,...
| | |