Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with converting code  RSS feed

 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..
previously I've managed to write this small piece of code in c# to simply convert floating numbers to readable fractions. I've tried to convert it to java in order to use it in an android project but doesn't seem to work. any idea on how it should be used in JAVA?
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Can you show us what you've tried on the Java version so far?
 
salvin francis
Bartender
Posts: 1604
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Welcome to code ranch.

I would suggest the following approach in the OOPs way :



Your C# code seems very close to Java except that we don't have var type in java and although there is a float primitive type in java, I think you can use doubles here.
Let us know if you are stuck somewhere
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Welcome to the Ranch!

Can you show us what you've tried on the Java version so far?


well i came to this conclusion and it seems to work fine:


but when i run in debug mode it appears that it doesn't exit the loop upon the first instance (which apparently should in the current example) and continues to examine all defined numbers. don't know why!?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:

I would suggest the following approach in the OOPs way :


thanks. can you please complete the class using the java code i have provided so far?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohsen Dehghani wrote:well i came to this conclusion and it seems to work fine:

Ah, so you want a float that approximates your fraction. That wasn't quite clear (I was thinking along the same lines as salvin).

Just one point, which might sound nitpicky: DON'T write
  float zojs = 1.0;
It's lazy, and can lead to hard-to-detect errors.

Why? Because 1.0 is a double, not a float; and while it won't cause any problems in this particular case, it can - and does - quite frequently.

The correct way to write the declaration above is:
  float zojs = 1.0f;

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say it would be better to declare that as a double, and forget there are such things as floats at all.
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mohsen Dehghani wrote:well i came to this conclusion and it seems to work fine:


The correct way to write the declaration above is:
  float zojs = 1.0f;



yeah. u're right . my fault. i was trying to give it a value for examining, although the value varies in the real application. i think it can also be written like: (float) 1 ;

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have all sorts of problems with the == operator. You might for example never manage to turn 0.1 into 1/10. That is because of the imprecision of floating‑point arithmetic.
Write yourself a method which tests how close a floating‑point number is to a ratio and use that:-If your numbers are 99 and 100 and your double only differs from the “correct” value by 0.0000001 return true. Consider some corner cases, e.g. numerator==0, ratio==0.0, numerator==denominator, ratio==1.0.
Maybe you should move !success out of the if block. I think you should not nest the loop inside the if, I think, but use !success as part of the continuation condition for your loops. Then when you have 3/2==1.5, you can terminate the loop rather than looking for 4/6.
 
salvin francis
Bartender
Posts: 1604
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohsen Dehghani wrote: i think it can also be written like: (float) 1 ;

I see it as an unnecessary type cast for a literal value.

Winston's suggestion above is the right approach:
float zojs = 1.0f;
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case OP didn't understand fully.

When you declare and initialize primitive variables either:
byte, short, int, long, float, double

no matter what data type you declare you always on the right side have either int or double, unless you specify type explicitly next to the number itself.

For instance:

You may want to read this tutorial. There you'll find this in more details as well as some other usefull information.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . . . .
Added later: That special exception only applies for values within the range of the “narrower” type, in the case of a byte −128…127.
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dah! You're right, many errors in that post, too many. Luckily we have many pairs of eyes in Ranch who watching us and doesn't let go errors sneakily in.

Mohsen Dehghani, please ignore my previous post and please accept my apologies for a poor post.
By trying to provide more details I went to the bow, don't know what I thought about 4B (I started in school in that class, but that is surely not that). And the other examples I gave (i.e.: line 1) were not good as I thought initially. Luckily compiler in the same way as Campbell wouldn't forgive that, so would complain.

Sorry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!