LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2010, 08:27 AM   #1
ishandutta2007
LQ Newbie
 
Registered: May 2009
Location: kolkata,india
Distribution: fedora
Posts: 6

Rep: Reputation: 0
Can any one please explain this output?


Code:
#include<cstdio>
#include<cmath>
int main(){
		printf("%lf\n", log(8)/log(2) );

		//case1:
		double v=log(8)/log(2);
		printf("%lld\n",(long long)v);
		
		//case2:
		printf("%lld\n",(long long)( log(8)/log(2) ));		

		//case3:
		printf("%lld\n",(long long)(double)( log(8)/log(2) ));
		
		//case4:
		printf("%lld\n",(long long)(float)( log(8)/log(2) ));		
}
output:
Code:
3.000000
3
2
2
3
Why is it not working properly while typecasting a double to long long,
but it works fine when we downcast double to float and then float to long long?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-22-2010, 08:39 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
32 bit x86 architecture uses 80 bit floating point (because it is most efficient) in some places you might expect fewer bits.

The 80 bit value log(8)/log(2) is probably a tiny amount less than 3. But when stored as 64 bit or 32 bit, it gets rounded to exactly 3.

I expect you compiled with less than max optimization, because the variable v seems to really exist as a 64 bit variable. More optimization could have eliminated v and used the 80 bit value instead.

The coded cast to double makes no difference, because the value log(8)/log(2) was already supposed to be a double and the fact that this double is in an 80 bit register is a quirk of the architecture.

The conversion directly from 80 bit floating point to 64 bit integer preserves the fact that the value is less than 3.

The cast to 32 bit float is a true conversion, so the rounding to exactly 3 occurs.

I think there are some compile time options that will generate slower code that covers most cases where 64 bit doubles would produce a different result than 80 bit (forces extra rounding of 80 bit values to 64 bit).

But, this type of problem is not limited to the 80 bit quirk of 32 bit x86 architecture. Similar problems are possible even in architectures with perfect IEEE 64 bit floating point. You should not rely on floating point computations on non integer values rounding back to exactly the correct integer. They might do so, but they might not.

log(8) cannot be exactly represented in any number of bits in floating point, nor can log(2). The approximation of log(8) has a good chance of not being exactly equal to 3 times the approximation of log(2). In such cases, the division should have about one chance in four of producing a result less than the correct integer. In simple cases like this, rounding to a floating point representation with at least 2 fewer bits (80 to 64 or either 80 or 64 to 32) will get you the exact correct integer result. But even that rule won't hold for slightly more complicated cases.

I think the difference between your cases 1 and 2 only occurs as a result of the 80 bit oddity in x86 architecture. But the difference between 2 and 4 can be a fundamental characteristic of floating point.

So with this or some other division that algebraically equals exactly 3, done on some architecture (or with some command line switches) to produce IEEE perfect results, you would find cases 2 and 3 are still what you consider "not working" but that case 1 joins them and is also "not working".

Last edited by johnsfine; 06-22-2010 at 08:57 AM.
 
2 members found this post helpful.
Old 06-22-2010, 03:48 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Wow !
 
Old 06-22-2010, 04:52 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
For a non-architecture specific analysis, go get a beverage and have a read of this ...
 
Old 06-23-2010, 01:16 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@johnsfine No doubt now. You really are a genius!

@syg00 That's quite a rare helpful document. Now adding it to my collection with MAF :-).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone explain this getfacl output? StupidNewbie Linux - Security 5 03-24-2010 01:01 PM
Explain the output(pointers) ishandutta2007 Programming 2 01-28-2010 08:25 AM
Could someone please explain this nmap output? stardotstar Linux - Networking 0 02-07-2005 10:02 PM
explain the output suchi_s Programming 9 11-08-2004 09:07 AM
explain the output c pragti Programming 10 05-28-2004 12:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration