C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Computing between different types.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
hongseok.yoon@gmail.com
Guest





PostPosted: Wed Sep 14, 2005 7:22 am    Post subject: Computing between different types. Reply with quote



Please let mem know the reason why bellow problem's occured...



float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

int i = 100;

f = i * f;

// At this point, f is 29 not 30!
// If I cast above i to float type ( f = static_cast< float >( i ) * f
),
// its result become 30 exactly as I expected.



What is the problem?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Sep 14, 2005 9:46 am    Post subject: Re: Computing between different types. Reply with quote



[email]hongseok.yoon (AT) gmail (DOT) com[/email] wrote:
Quote:
float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

It might print 0.3, but it can't be 0.3 exactly. The reason is that floats
are still binary numbers (unless you have one of those rare BCD encoded
machines). So, you can try to assemble the decimal value 0.3 from the
fractions of two, i.e. 1/2, 1/4, 1/8, 1/6 etc. However far you go, your
result will either be slightly larger or smaller.


Quote:
int i = 100;

f = i * f;

// At this point, f is 29 not 30!

I don't believe you. It might me slightly larger or smaller than 30, but not
exactly 29.

Quote:
What is the problem?

Floating point inaccuracy. Do not depend on absolute accuracy for floating
point operations.

Uli

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kwikius
Guest





PostPosted: Wed Sep 14, 2005 3:48 pm    Post subject: Re: Computing between different types. Reply with quote



[email]hongseok.yoon (AT) gmail (DOT) com[/email] wrote:
Quote:
Please let mem know the reason why bellow problem's occured...

[cut]

Quote:
What is the problem?

It would be helpful to know what compiler you are using, but
(incredibly) there is no problem (unless the int is not being converted
to a float before the calculation) . The C++ standard has no
specification describing what the results of a float computation should
be. The same is true for integers(except for the binary requirement)
and strings and characters. results of operations on all these types
come under the category 'implementation defined'. This is a result of
the C heritage, where abstract concepts were subservient to hardware
implementation requirements.

The 'implementation defined' feature distinguishes C++ from later
languages such as Java and C# where a primary goal is hardware
independence and where the specifications on results of operations on
inbuilt types are much more tightly controlled.

Hopefully though C++ is making some progress with ints (by means of the
int32 etc specs) and strings (e.g by design of a unicode library).
Unfortunately I dont think there are any proposals yet regarding
floating point specifications

regards
Andy Little


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Roberto Waltman
Guest





PostPosted: Thu Sep 15, 2005 1:09 am    Post subject: Re: Computing between different types. Reply with quote

[email]hongseok.yoon (AT) gmail (DOT) com[/email] wrote:
Quote:
Please let mem know the reason why bellow problem's occured...
...
// At this point, f is 29 not 30!
...
// its result become 30 exactly as I expected.

What is the problem?

Your expectations. - Read Goldberg's paper "What Every Computer
Scientist Should Know About Floating-Point Arithmetic"

( There is a copy here:
http://galileo.phys.virginia.edu/classes/551.jvn.fall01/goldberg.pdf )


Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jim Langston
Guest





PostPosted: Thu Sep 15, 2005 1:10 am    Post subject: Re: Computing between different types. Reply with quote


<hongseok.yoon (AT) gmail (DOT) com> wrote

Quote:
Please let mem know the reason why bellow problem's occured...



float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

int i = 100;

f = i * f;

// At this point, f is 29 not 30!
// If I cast above i to float type ( f = static_cast< float >( i ) * f
),
// its result become 30 exactly as I expected.



What is the problem?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


I think the answer can best be summarized by this program:

#include <iostream>

int main()
{
float MyFloat = 1.3;
int MyInt = MyFloat * 1000000;
std::cout << MyInt << std::endl;
char x;
std::cin >> x;
return 0;
}

Output in Microsoft Visual C++ .net 2003 is:
1299999

It seems that 1.3 can't be accurately described in binary.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carlos Moreno
Guest





PostPosted: Thu Sep 15, 2005 1:12 am    Post subject: Re: Computing between different types. Reply with quote

[email]hongseok.yoon (AT) gmail (DOT) com[/email] wrote:

Quote:
float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

int i = 100;

f = i * f;

// At this point, f is 29 not 30!

If f is 29, then you are not showing us the program that you
are running. With the code that you posted, f *can not* be 29
at this point.

If you do i = i * f, then it is perfectly possible that you
get a value of 29 in i -- the reason is that the result of
the operation is something *very close* to 30, but not
exactly 30. When converting to int (which is necessary if
you assign the result *to i*), the rule says that you
*truncate* (as opposed to round to the nearest integer).
If the result of the operation happens to be 29.9999999999
(which is possible), then when assigned to an int it would
be converted to 29.

HTH,

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Martin Bonner
Guest





PostPosted: Thu Sep 15, 2005 9:15 am    Post subject: Re: Computing between different types. Reply with quote


Carlos Moreno wrote:
Quote:
hongseok.yoon (AT) gmail (DOT) com wrote:

float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

int i = 100;

f = i * f;

// At this point, f is 29 not 30!

If f is 29, then you are not showing us the program that you
are running. With the code that you posted, f *can not* be 29
at this point.

Nit:

Actually, f COULD be 29.0 at this point - but that would imply an
incredibly awful quality-of-implementation for floating point.

(I agree that the OP almost certainly meant that i was 29 after i = i *
f)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Thu Sep 15, 2005 10:13 am    Post subject: Re: Computing between different types. Reply with quote

Ulrich Eckhardt wrote:
Quote:
hongseok.yoon (AT) gmail (DOT) com wrote:
float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

It might print 0.3, but it can't be 0.3 exactly. The reason is
that floats are still binary numbers (unless you have one of
those rare BCD encoded machines). So, you can try to assemble
the decimal value 0.3 from the fractions of two, i.e. 1/2,
1/4, 1/8, 1/6 etc. However far you go, your result will either
be slightly larger or smaller.

int i = 100;

f = i * f;

// At this point, f is 29 not 30!

I don't believe you. It might me slightly larger or smaller
than 30, but not exactly 29.

What is the problem?

Floating point inaccuracy. Do not depend on absolute accuracy
for floating point operations.

I suspect that Carlos Moreno hit the right answer, and he is in
fact assigning it to an int. If the results are slightly
smaller than 30, assigning them to an int will give 29.

A more interesting aspect is that he reported that casting i to
a float gave different results. According to the standard,
that's exactly what the compiler should do without the cast, so
there should be no difference. Of course, if he assigns the
results to a float, and then outputs them with the default
precision of 6, the results will probably be 30. Which is
different to the results of assigning the value to an int. But
there is also §5/10: "The values of the floating operands and
the results of floating expressions may be represented in
greater precision and range than that required by the type; the
types are not changed thereby." It's also possible that on his
machine, the compiler actually uses double (or even long double)
for the calculations, and then rounds them to float. This could
also explain slightly different results.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
hongseok.yoon@gmail.com
Guest





PostPosted: Thu Sep 15, 2005 10:17 am    Post subject: Re: Computing between different types. Reply with quote

I'm so sorry, code what I showed is not exactly same with I've tried.
You are right. I've casted ( i * f ) to int in order to get positive
number as a result.

I really appreciate your kind explanation include all of other people.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carlos Moreno
Guest





PostPosted: Thu Sep 15, 2005 1:56 pm    Post subject: Re: Computing between different types. Reply with quote

Martin Bonner wrote:
Quote:
Carlos Moreno wrote:

[email]hongseok.yoon (AT) gmail (DOT) com[/email] wrote:


float f = 1.3;
f -= 1.0;

// At this point, f is 0.3 if I print using cout.

int i = 100;

f = i * f;

// At this point, f is 29 not 30!

If f is 29, then you are not showing us the program that you
are running. With the code that you posted, f *can not* be 29
at this point.


Nit:

Actually, f COULD be 29.0 at this point - but that would imply an
incredibly awful quality-of-implementation for floating point.

Incidentally, I had initially written a tiny paragraph saying that
if it really was the case, then a severely-broken compiler etc etc;
but then, when re-reading the message before hitting SEND, I thought
it was simply too severely broken to be likely, so I deleted it :-)

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carlos Moreno
Guest





PostPosted: Thu Sep 15, 2005 1:57 pm    Post subject: Re: Computing between different types. Reply with quote

kanze wrote:

Quote:
[...]

I suspect that Carlos Moreno hit the right answer

Guys... guys... Why don't you call me just Carlos? If it was
someone that doesn't know me, ok -- but coming from someone I'm
sure knows me (from my posts, that is), it sounds so formal, so
strange that you use my firstname lastname... Not that it is
offensive or anything remotely similar, it just sounds strange :-)

It could be necessary, to disambiguate if there were two different
Carlos's that wrote in this thread, but that's not often the case
(it's not in this thread, at least)

Cheers,

Carlos Mor... errhhh, I mean, Carlos Wink
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.