473,698 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with multiplications of doubles and/or floats

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*do uble; (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

Jul 22 '05 #1
43 2198
Sorry, I forgot:

Linux (Debian) and gcc3.3

Jens

Jul 22 '05 #2
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*do uble; (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.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3
"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*do uble; (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
kb******@gascad .at
Jul 22 '05 #4
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*do uble; (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

Jul 22 '05 #5

"J.K. Becker" <jk******@becke r.de> wrote in message
news:c5******** **@news1.zdv.un i-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*do uble; (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
Jul 22 '05 #6
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*do uble; (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.

Jul 22 '05 #7
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*do uble; (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


Jul 22 '05 #8

"J.K. Becker" <jk******@becke r.de> wrote in message
news:c5******** **@news1.zdv.un i-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
Jul 22 '05 #9
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" <jk******@becke r.de> wrote in message
news:c5******** **@news1.zdv.un i-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


Jul 22 '05 #10

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

Similar topics

9
2569
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302 java: 325 gcc: 348 Python with Psyco: 1317 Pure Python using sum: 2312
3
1444
by: dave | last post by:
I'm trying to learn this. My problem, I have 2 strings that represent floats. I want to add them together. Should I look at using Cint to convert each character in the string and add individually? or would grouping the "digits" in the string by say 10 or 20 and working the smaller parts? Would i loose something by grouping in that way? Or is there a faster/better way? I have been thinking about this for a while and I don't want to start...
6
2545
by: Amit Bhatia | last post by:
Hello everyone. Sorry to be cross posting this on comp.lang.c++ (moderated) This is a simple question that is causing some problem in one of the classes that I have designed. I have two doubles: double a=2.4; double b=0.15; const double VERYTINY = 1.e-30; \\I define how small is small here;
14
1678
by: my.correo.basura | last post by:
Yesterday I found in a piece of code a float being incremented with ++. For some reason (wrong, I guess...) I thought that only integer types could be incremented that way, and I had never seen floats or doubles incremented that way before. Just to be sure I did a toy program and compiled with gcc -ansi -pedantic -Wall and it worked without any error or warning, showing the correct result. Is it correct to increment floats or doubles with...
6
8716
by: Mike P | last post by:
Why does this cause the error 'cannot implicitly convert type 'double' to 'float'? Can you not multiply doubles by floats without converting them all to the same datatype? fltPCRateStd = fltPCRateStd * 1.175; Regards, Mike
10
2423
by: abdul_n_khan | last post by:
I have a basic question related to datatype conversion. I am multiplying currency to float datatype. fltInterestRate=1.23333; curAmount = 91000000; curInterestAmount = curAmount * fltInterestRate curInterestAmount should have 112233030
7
3881
by: SpreadTooThin | last post by:
I have some code... import array a = array.array('d') f = open('file.raw') a.fromfile(f, 10) now I need to convert them into floats (32 bit...) what do i do?
6
2906
by: Pavel | last post by:
Hello, Does anyone know a (preferably open-source) multi-platform C or C++ library that would be able to write and read C/C++ doubles and floats to/from streambuf, char array or similar device in IEEE 754 with reasonably optimal precision and performance? The purpose is to exchange serialized doubles and floats between C/C++ and Java programs where Java serialization rules are used.
3
1927
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.