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:

Turbo Incremental Link 5.60 Copyright (c) 1997-2002 Borland

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