LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-31-2003, 04:31 PM   #1
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Java | C | C++ - Float/Double question


Hi again!

I've been playing a little today with Java, C and C++ and I've found something ratter interesting. Though, I could not find a reason for why this is happening.
Those 4 programs bellow (two in java one in C and the other one in C++) does exactly the very same simple thing. They will get a float value, convert it explicitly into an integer and the value (will be 10) will call an if-else case.

The thing is, with both C and C++ it works, but the one with Java does not. Do make it work, I've to use double instead of float. Question is... why? Since the float is a very small number (10.3) and should fit into float's bounds. Here they come, first in C:

Code:
// prog1.c
// Program show a simple float-to-integer explicit converting.

#include <stdios.h>

int main()
{

    // Declare variables
    float someFloat = 10.3;
    int someInt = (int) someFloat;     // here will convert float to int

    if ( someInt > 10 ){
        printf("%s", "someInt is bigger then 10");
    } else if ( someInt == 10 )
        printf("%s", "someInt is exactly 10");
    } else {
        printf("%s", "someInt is smaller then 10");
    }

    return 0;
}
Now in C++

Code:
// prog1.cpp
// Program shows a simple float-to-integer explicity converting

#include <iostream>
using namespace std;

int main()
{

    // Declaring variables
    float someFloat = 10.3;
    int someInt = (int)someFloat;   // converting int to float...

    if ( someInt > 10 ) {
        cout << " someInt is bigger then 10\n ";
    } else if ( someInt == 10 ) {
        cout << " someInt is exactly 10\n";
    } else {
        cout << " someInt is smaller then 10\n";
    }

    return 0;

}
As you can see, this is pretty possible in both C and C++. Now watch Java code closely. The following program will not compile and return error... saying that double was found, but expecting a float (?)


Code:
class prog1 
{

    public static void main ( String[] arguments )
    {

        // Declaring stuff
        float someFloat = 10.3;
        int someInt = (int)someFloat;    // converting...

        if ( someInt > 10 ) {
            System.out.println ( "someInt is bigger then 10");
        } else if ( someInt == 10 ) {
            System.out.println ("someInt is exactly 10");
        } else {
            System.out.println ("someInt is smaller than 10");
        }
    }
}

As I said above, this program will not compile. But the next one will...

Code:
class prog1
{

    public static void main ( String[] arguments )
    {
        // Declaring stuff
        double someDouble = 10.3;
        int someInt = (int) someDouble;

        if ( someInt > 10 ) {
            System.out.println ( "someInt is bigger then 10");
        } else if ( someInt == 10 ) {
            System.out.println ("someInt is exactly 10");
        } else {
            System.out.println ("someInt is smaller than 10");
        }
    }
}
I know I know... they are all hard-coded as well . I just wanted to know why Java won't accept to convert float to integer since 10.3 is a pretty small number still under float bounds, but does, accept it as being double? Any ideas?

Thanks in advance

P.S: I've not copied-paste the code above. I've just wrote it here at the forum (I'm on win now and I've no compilers at all in this machine) so there might be some errors above .

Happy New Year Everybody!!!
 
Old 12-31-2003, 05:11 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
it can be fixed by replacing 10.3 with 10.3f to tell the complier that 10.3 should be treated as a float, i dont know for sure(i dont know java) but it seems that decimal constants are assumned to be doubles.
 
Old 12-31-2003, 06:04 PM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
yup That surely did the trick Thanks a lot mate. I personally like Java the most, but it can be pretty bitching sometimes :S. Specially when it comes to to pointers. They say Java does not have one, when it actually does, use pointers. Java still has a long way to grow...

Thanks again
 
Old 01-01-2004, 08:13 AM   #4
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
You are correct - Java for sure does have pointers. However, when people say "Java does not have pointers", I think it's just a lazy way of saying that you cannot do pointer arithmetic in Java - which is true.
 
Old 01-01-2004, 08:49 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
it is my limited understand that if a variable type derives in some way from Object then when you declare it you are declaring a pointer to it and it has to be new'ed before use, but these pointers are constant and you cant do arithmatic with them. this in my opinion is one of the advantages of java because it allows it to easily run in a 'sandbox'
 
Old 01-01-2004, 09:51 AM   #6
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
sounds good to me
 
Old 01-01-2004, 10:14 AM   #7
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Hey Good points guys. Gonna use it at the school and impress my fellows ghehe
 
  


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
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
c++: float to double conversion ashirazi Programming 6 11-30-2004 04:14 PM
can't convert o to FLoat in Java alaios Programming 4 04-24-2004 02:10 PM
Float/Double to String? Mega Man X Programming 16 01-03-2004 07:50 PM
java covert double into string iceman47 Programming 10 05-28-2003 10:51 PM

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

All times are GMT -5. The time now is 01:10 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