Velocity Reviews > C++ > Problems with multiplications of doubles and/or floats

# Problems with multiplications of doubles and/or floats

J.K. Becker
Guest
Posts: n/a

 04-14-2004
Hi there,

I am trying to multiply doubles with floats (actually I tried every
possible combination by now) and it never works (well, it does something
but it is always wrong). I have no idea what it is and where to look for
help, maybe some of you know?

double=float*double; (or every possible combination of it). An example:

0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
If I change the types of the variables I think the result stays the same
(but I am not 100% sure)...

Jens

J.K. Becker
Guest
Posts: n/a

 04-14-2004
Sorry, I forgot:

Linux (Debian) and gcc3.3

Jens

Thomas Matthews
Guest
Posts: n/a

 04-14-2004
J.K. Becker wrote:
> Hi there,
>
> I am trying to multiply doubles with floats (actually I tried every
> possible combination by now) and it never works (well, it does something
> but it is always wrong). I have no idea what it is and where to look for
> help, maybe some of you know?
>
> double=float*double; (or every possible combination of it). An example:
>
> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
> If I change the types of the variables I think the result stays the same
> (but I am not 100% sure)...
>
> Jens
>

I don't get the same answer:
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
\$ cat junk.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
const double three_tenths = 0.3;
const double seven_tenths = 0.7;
const float f_three_tenths = 0.3;
const float f_seven_tenths = 0.7;

printf("direct multiplication: 0.3 * 0.7 = %f\n", 0.3 * 0.7);
printf("double * double: %f\n", three_tenths * seven_tenths);
printf("float * float: %f\n", f_three_tenths * f_seven_tenths);
printf("double * float: %f\n", three_tenths * f_seven_tenths);
printf("float * double: %f\n", f_three_tenths * seven_tenths);
return EXIT_SUCCESS;
}

TH009MA@th009ma-shl2-01 /cygdrive/d/temp
\$ gcc --version
gcc (GCC) 3.3.1 (cygming special)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

TH009MA@th009ma-shl2-01 /cygdrive/d/temp
\$ ./junk
direct multiplication: 0.3 * 0.7 = 0.210000
double * double: 0.210000
float * float: 0.210000
double * float: 0.210000
float * double: 0.210000

************************************************

TH009MA@th009ma-shl2-01 /cygdrive/d/temp
\$ bcc32 junk.c
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
junk.c:

TH009MA@th009ma-shl2-01 /cygdrive/d/temp
\$ ./junk
direct multiplication: 0.3 * 0.7 = 0.210000
double * double: 0.210000
float * float: 0.210000
double * float: 0.210000
float * double: 0.210000

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Karl Heinz Buchegger
Guest
Posts: n/a

 04-14-2004
"J.K. Becker" wrote:
>
> Hi there,
>
> I am trying to multiply doubles with floats (actually I tried every
> possible combination by now) and it never works (well, it does something
> but it is always wrong). I have no idea what it is and where to look for
> help, maybe some of you know?
>
> double=float*double; (or every possible combination of it). An example:
>
> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?

Yes. Post the program. You have a bug in it.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

John Harrison
Guest
Posts: n/a

 04-14-2004

"J.K. Becker" <(E-Mail Removed)> wrote in message
news:c5jfaq\$reo\$(E-Mail Removed)-mainz.de...
> Hi there,
>
> I am trying to multiply doubles with floats (actually I tried every
> possible combination by now) and it never works (well, it does something
> but it is always wrong). I have no idea what it is and where to look for
> help, maybe some of you know?
>
> double=float*double; (or every possible combination of it). An example:
>
> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?

No that is impossible, you have a bug somewhere else in your program. Since
you didn't post the program its impossible to help. Post the code.

What else did you expect, did you think someone would say 'oh yes that's the
well known 0.7 * 0.3 bug, it's been fixed in the new compiler version'?
Compilers do not make basic arithmetic errors. If you want help with a buggy
program you have to post the program code, there is no other way.

See the FAQ http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

john

Rolf Magnus
Guest
Posts: n/a

 04-14-2004
J.K. Becker wrote:

> Hi there,
>
> I am trying to multiply doubles with floats (actually I tried every
> possible combination by now) and it never works (well, it does
> something but it is always wrong). I have no idea what it is and where
> to look for help, maybe some of you know?
>
> double=float*double; (or every possible combination of it). An
> example:
>
> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any
> idea? If I change the types of the variables I think the result stays
> the same (but I am not 100% sure)...

Works here:

#include <iostream>

int main()
{
float a = 0.3;
double b = 0.7;

std::cout << a << " * " << b << " = " << a * b << '\n';
}

Output:

0.3 * 0.7 = 0.21

Matt
Guest
Posts: n/a

 04-15-2004
J.K. Becker wrote:
> Hi there,
>
> I am trying to multiply doubles with floats (actually I tried every
> possible combination by now) and it never works (well, it does something
> but it is always wrong). I have no idea what it is and where to look for
> help, maybe some of you know?
>
> double=float*double; (or every possible combination of it). An example:
>
> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
> If I change the types of the variables I think the result stays the same
> (but I am not 100% sure)...
>
> Jens
>

The problem is in your print statement or output statement.

man fprintf.

J.K. Becker
Guest
Posts: n/a

 04-15-2004
Oh well, you got it all wrong! This stupid calculation was just an
example, it does not matter what the numbers are, the result is always
wrong. When I asked for help I was looking for something in the line of
"you could have a memory leak" (of course I checked that already) or
"this always happens on sunny days with gcc3.3, try gcc0.1prealpha" or
something.
The program is far to long to post here, if you really want to look at
it have a look at the working parts first here

https://sourceforge.net/projects/elle/

or a description with links to all kinds of stuff there:

http://www.microstructure.uni-tuebin...enix/index.php

And now that you know that I am not completely new to c++, no more rtfm,
or man printf, I think I am past that by now...

Jens

J.K. Becker wrote:
> Hi there,
>
> I am trying to multiply doubles with floats (actually I tried every
> possible combination by now) and it never works (well, it does something
> but it is always wrong). I have no idea what it is and where to look for
> help, maybe some of you know?
>
> double=float*double; (or every possible combination of it). An example:
>
> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
> If I change the types of the variables I think the result stays the same
> (but I am not 100% sure)...
>
> Jens
>

John Harrison
Guest
Posts: n/a

 04-15-2004

"J.K. Becker" <(E-Mail Removed)> wrote in message
news:c5lglu\$ul3\$(E-Mail Removed)-mainz.de...
> Oh well, you got it all wrong! This stupid calculation was just an
> example, it does not matter what the numbers are, the result is always
> wrong. When I asked for help I was looking for something in the line of
> "you could have a memory leak" (of course I checked that already) or
> "this always happens on sunny days with gcc3.3, try gcc0.1prealpha" or
> something.
> The program is far to long to post here, if you really want to look at
> it have a look at the working parts first here
>
> https://sourceforge.net/projects/elle/
>
> or a description with links to all kinds of stuff there:
>
> http://www.microstructure.uni-tuebin...enix/index.php
>
> And now that you know that I am not completely new to c++, no more rtfm,
> or man printf, I think I am past that by now...
>

Well how was anyone supposed to know that? We get lots of clueless newbies
posting here. One reason for posting source code is that it makes it easy
for people to assess your level of expertise.

The answer is still to post source code. Take your large program and cut out
the irrelevant parts. When doing this one of two things will happen. You may
remove some code you don't think is relevant and the problem goes away, this
is a good clue as to where the bug is. The other thing that might happen is
that you get down to a smallish program that still exhibits the behaviour
you don't understand, then you can post that code here and someone will fix
it.

This is all explained in the FAQ

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Take the trouble, and you'll get your problem fixed.

john

J.K. Becker
Guest
Posts: n/a

 04-15-2004
I can't take the program apart, that would be more work than
reprograming the part that is not working. There is a reason for me not
to post the progam here, you know, believe me, if I could I would. I can
post the function here, but that is not going to help.
If you insist I will.

John Harrison wrote:
> "J.K. Becker" <(E-Mail Removed)> wrote in message
> news:c5lglu\$ul3\$(E-Mail Removed)-mainz.de...
>
>>Oh well, you got it all wrong! This stupid calculation was just an
>>example, it does not matter what the numbers are, the result is always
>>wrong. When I asked for help I was looking for something in the line of
>>"you could have a memory leak" (of course I checked that already) or
>>"this always happens on sunny days with gcc3.3, try gcc0.1prealpha" or
>>something.
>>The program is far to long to post here, if you really want to look at
>>it have a look at the working parts first here
>>
>>https://sourceforge.net/projects/elle/
>>
>>or a description with links to all kinds of stuff there:
>>
>>http://www.microstructure.uni-tuebin...enix/index.php
>>
>>And now that you know that I am not completely new to c++, no more rtfm,
>>or man printf, I think I am past that by now...
>>

>
>
> Well how was anyone supposed to know that? We get lots of clueless newbies
> posting here. One reason for posting source code is that it makes it easy
> for people to assess your level of expertise.
>
> The answer is still to post source code. Take your large program and cut out
> the irrelevant parts. When doing this one of two things will happen. You may
> remove some code you don't think is relevant and the problem goes away, this
> is a good clue as to where the bug is. The other thing that might happen is
> that you get down to a smallish program that still exhibits the behaviour
> you don't understand, then you can post that code here and someone will fix
> it.
>
> This is all explained in the FAQ
>
> http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
>
> Take the trouble, and you'll get your problem fixed.
>
> john
>
>