Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2007

Tip: Looking for answers? Try searching our database.

Translate C expression to Java expression

Thread view: 
Jeff Higgins - 26 Nov 2007 03:15 GMT
Hi,
 I'm attempting to translate a C function to a Java method,
but I'm stuck on the two non-trivial  return expressions  shown below.
Hoping someone here can help.
Thanks,
Jeff Higgins

int test(double* xy1, double* xy2, double* xy3)
{
 if (fabs(vectmult(xy1, xy2, xy3)) != 0.0)
   return 0;
 if (xy1[0] != xy2[0])
   return (xy3[0] - xy2[0]) * (xy2[0] - xy1[0]) < 0.0;
 if (xy1[1] != xy2[1])
   return (xy3[1] - xy2[1]) * (xy2[1] - xy1[1]) < 0.0;
 return 1;
}
Stefan Ram - 26 Nov 2007 03:25 GMT
>int test(double* xy1, double* xy2, double* xy3)
>{
[quoted text clipped - 6 lines]
>  return 1;
>}

 untested:

boolean test(double[] xy1, double[] xy2, double[] xy3)
{
 if (Math.abs(vectmult(xy1, xy2, xy3)) != 0.0)
   return false;
 if (xy1[0] != xy2[0])
   return (xy3[0] - xy2[0]) * (xy2[0] - xy1[0]) < 0.0;
 if (xy1[1] != xy2[1])
   return (xy3[1] - xy2[1]) * (xy2[1] - xy1[1]) < 0.0;
 return true;
}
Jeff Higgins - 26 Nov 2007 03:48 GMT
>>int test(double* xy1, double* xy2, double* xy3)
>>{
[quoted text clipped - 19 lines]
>  return true;
> }

Thanks Stefan, for your reply.
I guess the trouble I'm having is:

int test()
{
 double a = 3.0;
 double b = 6.0;
 return a * b > 0;
}

does not compile: Type mismatch:
cannot convert from boolean to int.
Patricia Shanahan - 26 Nov 2007 03:59 GMT
>>> int test(double* xy1, double* xy2, double* xy3)
...

>> boolean test(double[] xy1, double[] xy2, double[] xy3)
...

> Thanks Stefan, for your reply.
> I guess the trouble I'm having is:
[quoted text clipped - 8 lines]
> does not compile: Type mismatch:
> cannot convert from boolean to int.

C does not have a boolean data type, and uses int instead. C relational
expressions produce an int result.

Java has a separate boolean data type. If test is intended to return a
true or false result, as appears to be the case, it should be declared
to return boolean.

Note that Stefan's suggested code included that change.

Patricia
Ulrich Eckhardt - 26 Nov 2007 09:46 GMT
> C does not have a boolean data type, and uses int instead.

Yes it does, it is called _Bool. Historically and practically though you are
right that 'int' is often used to carry a simply yes/no information. Better
code uses a typedef though.

Uli

Signature

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Patricia Shanahan - 26 Nov 2007 10:05 GMT
>> C does not have a boolean data type, and uses int instead.
>
> Yes it does, it is called _Bool. Historically and practically though you are
> right that 'int' is often used to carry a simply yes/no information. Better
> code uses a typedef though.

I sit corrected. Obviously my C knowledge is out of date.

Patricia
Lew - 26 Nov 2007 14:01 GMT
Ulrich Eckhardt wrote:
>>> C does not have a boolean data type, and uses int instead.
>>
[quoted text clipped - 3 lines]
>> Better
>> code uses a typedef though.

A typedef doesn't create a new type, it only aliases an existing one.

> I sit corrected. Obviously my C knowledge is out of date.

From
<http://home.datacomm.ch/t_wolf/tw/c/c9x_changes.html>
> Objects of the new boolean type _Bool may have one of the two values zero or one.
Note that this is C99 only.

It's still an int.

C does not have a boolean type.  Patricia, you may sit uncorrected.  Obviously
your C knowledge is still valid.

<http://std.dkuug.dk/jtc1/sc22/open/n2794/n2794.txt>
>   [#2] An object declared as type _Bool  is  large  enough  to
>        store the values 0 and 1.
...
> The  type  _Bool  and   the
>        unsigned  integer  types  that  correspond  to  the standard
>        signed integer  types  are  the  standard  unsigned  integer
>        types.  

Signature

Lew

Ulrich Eckhardt - 26 Nov 2007 15:06 GMT
> Ulrich Eckhardt wrote:
>>>> C does not have a boolean data type, and uses int instead.
[quoted text clipped - 4 lines]
>
> A typedef doesn't create a new type, it only aliases an existing one.

... and makes code clearer! When I read

 boolean foo();

...I immediately know that it returns a boolean value, regardless of whether
that type is a separate builtin type or just an alias. Compared with

 int foo();

...where I have to look up documentation to find if the integral number
returned is in fact considered a boolean (1=success) or an errorcode (0=no
error).

Of course much more elegant is to not return anything in case of failure but
to throw an exception. ;)

>> I sit corrected. Obviously my C knowledge is out of date.
>
[quoted text clipped - 3 lines]
>> or one.
> Note that this is C99 only.

Yes, and that is the latest C standard and that's of course what I'm
referring to. If you mean that lots of compilers and code don't implement
that, that is another thing.

> It's still an int.

No. Look again at the link you gave: '_Bool' is a _keyword_ in C. What is a
typedef is 'bool', which you get if you #include <stdbool.h>. Then, 'bool'
is an alias for '_Bool'.

Note that the last text you quoted in fact stated that _Bool was in fact
considered an unsigned integral type, but I assume that is not what you
meant with "It's still an int." above.

Uli

Signature

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Lew - 26 Nov 2007 15:22 GMT
> Note that the last text you quoted in fact stated that _Bool was in fact
> considered an unsigned integral type, but I assume that is not what you
> meant with "It's still an int." above.

Strange assumption, since it *is* exactly what I meant.  That quote was just
to prove the point.  Why would you conclude the exact opposite?

Signature

Lew

Lew - 26 Nov 2007 15:27 GMT
>> Ulrich Eckhardt wrote:
>>>>> C does not have a boolean data type, and uses int instead.
[quoted text clipped - 5 lines]
> .... and makes code clearer! When I read
>   boolean foo();

I don't deny that it makes reading code easier, but it doesn't change the fact
that C does not have a boolean type, but rather uses integral types to drive
conditional logic.

> ....I immediately know that it returns a boolean value, regardless of whether

Actually, it returns an integral value which can drive conditionals.  For
proof, just try to return 0 or 1 from such a method.

> that type is a separate builtin type or just an alias. Compared with

The fact is that you are returning an int, and can use "return 1;" in the code.

Lew wrote:
>> It's still an int.

> No. Look again at the link you gave: '_Bool' is a _keyword_ in C. What is a

I never said that "_Bool" was a typedef.  I said that it is an int.  Straw man.

Signature

Lew

Ulrich Eckhardt - 26 Nov 2007 15:50 GMT
>>> It's still an int.
>
>> No. Look again at the link you gave: '_Bool' is a _keyword_ in C. What is
>> a
>
> I never said that "_Bool" was a typedef.  I said that it is an int.

'_Bool' and 'int' are separate types, '_Bool' is not an 'int'!

>> Note that the last text you quoted in fact stated that _Bool was in fact
>> considered an unsigned integral type, but I assume that is not what you
>> meant with "It's still an int." above.
>
> Strange assumption, since it is exactly what I meant.  That quote was
> just to prove the point.  Why would you conclude the exact opposite?

I was assuming that when you wrote 'int' you meant the C type 'int' and
not 'integral value' in general. The reason is that traditionally the
type 'int' was often used to represent a boolean value.

If you want to argue whether it is smart to treat a boolean value as
integral value, that is another topic. If you only wanted to point out that
C's boolean is treated as an integral value you are right, though I find
your wording unfortunate.

BTW: I find calling an argument a straw man here is pretty respectless as it
implies malice to me. At least as far as I am concerned I don't call you
names and I also don't assume that you are intentionally misrepresenting
things. You could give me the same respect.

Uli

Signature

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Lew - 26 Nov 2007 15:58 GMT
> BTW: I find calling an argument a straw man here is pretty respectless as it
> implies malice to me. At least as far as I am concerned I don't call you
> names and I also don't assume that you are intentionally misrepresenting
> things. You could give me the same respect.

There is no implication of malice on your part.  How do you derive that?

It was an objective assessment *of your argument* - you attributed a point to
me that I did not make, namely an incorrect labeling of '_Bool' as a typedef,
which I did not do.  I did not call anyone any names.  Accusing me of calling
names is also a straw man argument, since I didn't do that either.

<http://en.wikipedia.org/wiki/Straw_man>

Signature

Lew

Ulrich Eckhardt - 26 Nov 2007 17:12 GMT
>> BTW: I find calling an argument a straw man here is pretty respectless as
>> it implies malice to me. At least as far as I am concerned I don't call
>> you names and I also don't assume that you are intentionally
>> misrepresenting things. You could give me the same respect.
>
> There is no implication of malice on your part.  How do you derive that?

Calling an argument a straw man implies _TO ME_ that the argument
intentionally missed the point. I repeat: it means that _TO ME_. Maybe
that's just the interpretation of a non-native English speaker.

> It was an objective assessment *of your argument* - you attributed a point
> to me that I did not make, namely an incorrect labeling of '_Bool' as a
> typedef, which I did not do.

Lew, are you trolling or do you just not understand? I cut you some slack
until now, but as already said your behaviour is simply annoying. If you
took the time to actually read and understand what I said, and maybe ask
instead of assuming things, you would see this:

1. You wrote: "It's still an int.", where 'it' can only refer to the
mentioning of C's _Bool type in a sentence you quoted. IOW you said that
a '_Bool' is an 'int'.

2. I said that it is a keyword. Note that this doesn't directly attack your
argument, rather it mentions the fact that it's a keyword in order to show
that 'int' and '_Bool' are separate types and thus implicitly that
a '_Bool' is not an 'int'. If you really meant 'integral value' when you
wrote 'int' then you simply need to pay better attention to what you are
saying so that it better corresponds to what you mean.

3. To that you answer:
| I never said that "_Bool" was a typedef.  I said that it
| is an int.  Straw man.

Which clearly shows that you either didn't understand or chose to ignore
what I said. I did for example never say that you said it was a typedef.
Really, go back and read it, there is no such claim! In fact if I could
have called that claim of yours an invalid argument, but I didn't do that
because I first assume a misunderstanding, a simple communication error and
not that you are trolling.

> I did not call anyone any names.  Accusing me of calling names is
> also a straw man argument, since I didn't do that either.

You are completely missing the point of a straw man, which is to make a
pseudo-argument. However, I did not make an argument, I merely pointed out
that I consider your behaviour questionable.

Please, read the above quoted paragraph ("BTW: I find..") again: I wasn't
arguing anything, I was merely pointing out a fact, namely how I perceived
your behaviour. Please pay attention to things like "I find" or "to me". I
intentionally worded it like that to make sure that it is understood as a
subjective, personal statement.

Uli

Signature

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Lew - 26 Nov 2007 22:59 GMT
> Please, read the above quoted paragraph ("BTW: I find..") again: I wasn't
> arguing anything, I was merely pointing out a fact, namely how I perceived
> your behaviour. Please pay attention to things like "I find" or "to me". I
> intentionally worded it like that to make sure that it is understood as a
> subjective, personal statement.

You are absolutely right in all regards.

Signature

Lew

Ulrich Eckhardt - 27 Nov 2007 12:27 GMT
>> Please, read the above quoted paragraph ("BTW: I find..") again: I wasn't
>> arguing anything, I was merely pointing out a fact, namely how I
[quoted text clipped - 3 lines]
>
> You are absolutely right in all regards.

Thank you for confirming that my perceptions are actually what I said they
were.

Uli

Signature

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Jeff Higgins - 27 Nov 2007 23:57 GMT
>> You are absolutely right in all regards.
>
> Thank you for confirming that my perceptions are actually what I said they
> were.

Now, who pissed higher.
<http://www.figherfighters.wanted
Roedy Green - 26 Nov 2007 04:30 GMT
On Sun, 25 Nov 2007 22:48:26 -0500, "Jeff Higgins"
<oohiggins@yahoo.com> wrote, quoted or indirectly quoted someone who
said :

>int test()
>{
>  double a = 3.0;
>  double b = 6.0;
>  return a * b > 0;

you compute a boolean and attempt to return an int.

The most logical change would be

boolean test ()

or you might do

return a * b > 0 ? 1 : 0 ;
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Jeff Higgins - 26 Nov 2007 10:57 GMT
> but I'm stuck
Thanks to all the respondents.
Stefan's change to return a boolean will work.

I apologise for the confusion. For some reason
I was thinking that the code (in it's full context)
was expecting a comparator type return value(-1, 0, 1).
This turns out to be not the case.

Coders Home page:
<http://www.marine.csiro.au/~sak007/>

Code located:
<http://www.marine.csiro.au/~sakov/gridgen.tar.gz>
issimplepoly.c _sl_intersect


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.