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

Internal representation of doubles

Recently I coded something for a class that uses a LOT of doubles with
precision out to the thousands place. I did most of the arithmetic by
multiplying each by a thousand and converting to an int, but not all of
it. In any case, coding it under FreeBSD, I didn't have any problems.
However, when I moved it Linux and compiled, I got drastically
different numbers. I eventually fixed the problem by multiplying
everything by 1000 and storing as ints, and then converting back when
printing to the screen, but I'm not sure even now if I fully understand
why this happened. Is this a compiler thing? I was using 3.3.3 on
freebsd and 3.3.2 on linux... Or is this an OS thing?

Jul 23 '05 #1
7 1507
jd******@gmail.com wrote:
Recently I coded something for a class that uses a LOT of doubles with
precision out to the thousands place. I did most of the arithmetic by
multiplying each by a thousand and converting to an int, but not all of
it. In any case, coding it under FreeBSD, I didn't have any problems.
However, when I moved it Linux and compiled, I got drastically
different numbers.
You'd have to define "drastically".
I eventually fixed the problem by multiplying
everything by 1000 and storing as ints, and then converting back when
printing to the screen, but I'm not sure even now if I fully understand
why this happened. Is this a compiler thing? I was using 3.3.3 on
freebsd and 3.3.2 on linux... Or is this an OS thing?


It can be all of those things, and a hardware thing. It does sound weird,
though. Most probably it's the rounding that was different. It could be
a simple truncation on one and true rounding on the other. And the
rounding could be towards nearest value, towards zero, etc.

BTW, why did you have to do the "multiply by 1000 and convert to int"
dance?

V
Jul 23 '05 #2
Well, by multiplying by 1000 and converting to int, it seemed to make
the rounding a little more consistent. Its one of these things where
my output needs to match my professors exactly and through thousands of
iterations of arithmetic with a few numbers, small differences in
rounding compounded to produce large differences.

Jul 23 '05 #3
jd******@gmail.com wrote:
Well, by multiplying by 1000 and converting to int, it seemed to make
the rounding a little more consistent. Its one of these things where
my output needs to match my professors exactly and through thousands of
iterations of arithmetic with a few numbers, small differences in
rounding compounded to produce large differences.


Ah... In this case you should put additional code into your program
that in the beginning will ask for the desired output and then when
the program is finished, will just output precisely what was desired.
Come to think of it, the code between inputting the "right" numbers
and outputting them could simply be "sleep(10000);"...

V
Jul 23 '05 #4
In article <11********************@z14g2000cwz.googlegroups.c om>,
jd******@gmail.com <jd******@gmail.com> wrote:
Well, by multiplying by 1000 and converting to int, it seemed to make
the rounding a little more consistent. Its one of these things where
my output needs to match my professors exactly and through thousands of
iterations of arithmetic with a few numbers, small differences in
rounding compounded to produce large differences.


That is the nature of limited precision arithmetic. People confuse it
for real number arithmetic all the time. If this is important to you,
you can find a primer entitled: "What Every Computer Scientist Should
Know About Floating-Point Arithmetic"

HTML version here:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

It's important to know that the C++ language does not specify
implementation details sufficiently for a professor to require results
to be the same everywhere. That is intentional, as different
architectures are free to decide how to implement FP arithmetic
without making it impossible for compilers on those architectures to
use FP hardware.
--
Mark Ping
em****@soda.CSUA.Berkeley.EDU
Jul 23 '05 #5
<jdog...@gmail.com> wrote...
[...] Its one of these things where
my output needs to match my professors exactly and through thousands of
iterations of arithmetic [...].


Option 1: Run the same code as your professor on the same hardware running the same software

Option 2: Explain to your professor the niceties of floating-point arithmetic

;-)

--
Lionel B
Jul 23 '05 #6

jd******@gmail.com wrote:
Well, by multiplying by 1000 and converting to int, it seemed to make
the rounding a little more consistent. Its one of these things where
my output needs to match my professors exactly and through thousands of iterations of arithmetic with a few numbers, small differences in
rounding compounded to produce large differences.


Seems to me that by explicitly rounding to the 3rd decimal, *you* are
the one introducing the rounding errors. Unless you realy *need* data
that is confined to a limited precision, like currency (only 2 decimal
places), currency exchange rates (5) or integer data, all rounding
should be done as late in the process as possible - i.e., only at the
end. Up to that point, why not profit from all the accuracy your
platform can give you in a double variable?

As to compounding roundoff errors accumulating in a large difference,
you may want to check whether your algorithm as a whole is stable. If
it were stable, small changes in initial conditions should only yield
small changes in the end result. Small roundoff errors would then not
be able to propagate far.

regards Mark

Jul 23 '05 #7
I think my professor is well aware of how floating point arithmetic
works, and its quirks. His suggestion for solving the problem was to
simply do the same thing that he did (and he told us how he did it, and
gave us example input and ouput). Also, since we had to submit
statically compiled binaries, along with source, compilers will not be
an issue.

I guess when I said "exactly" the same, I didnt' really mean that. It
just needs to be very close. He's a pretty reasonable guy.

It is true that I am causing such "rounding errors" or whatever they
are by explicitly rounding to the third decimal, but this was his
approach, so he recommended we do the same for consistency, especially
since precision beyond the thousands place was trivial.

Thank you all for your comments.

Jul 23 '05 #8

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

Similar topics

9
by: Arun Goel | last post by:
Hi, I came across this question from a website.. According to the C++ standard, what is an object's internal representation in memory guaranteed to be? a) Initialized b) On a word boundary...
18
by: Mantorok Redgormor | last post by:
What does a trap representation mean in the standard? And how can ~0 cause a trap representation? Could someone point out the relevant sections in the standard?
17
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
5
by: Sathyaish | last post by:
When you say char in C, it internally means "an unsigned small integer with 1-byte memory", right? More importantly, the internal representation of char does not mean "int" as in...
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
5
by: Nikeman | last post by:
good day, I simply would like to know as to how I can parse seconds to convert them into equivalent DateTime representation. To be more persice, I have a DirectShow app written in VB.NET, with a...
3
by: CDMAPoster | last post by:
A.K.A. Is Double Dating a bad thing :-)? My post from several hours ago may have gotten lost so please forgive me if something similar to this shows up twice. From a modular programming class I...
9
by: archana | last post by:
Hi all, I have one question regarding hashing in .net I have two string containing same data. When i see hashcode by calling gethascode, i am getting same value. I want to know how...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.