Results 1 to 12 of 12
Thread: Double with no negative values.
- 05-03-2011, 05:20 AM #1
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
- 05-03-2011, 05:26 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The Java Language Environment
Short answer: no. You can use conditionals to prevent it from being negative though.
- 05-03-2011, 11:16 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
From my understanding you wouldn't declare 'double not negative' but declare a double and then set a condition that if it is negative the program prompts the user to enter a different double.
If it's part of a program and isn't receiving user input then either convert it to a positive (all I'd do is multiply it by 2 then divide by 2, loss of a little accuracy perhaps though) or set some sort of
condition on it.Java Code:if(i<0)
~Karenthian~
- 05-03-2011, 11:27 AM #4
- 05-04-2011, 08:52 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
Sorry, what I should have written is either times by -2 or squared. My bad.
~Karenthian~
- 05-04-2011, 08:56 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
An easier approach would be to multiply it by -1. perhaps it's even possible with the unary operator +
- 05-04-2011, 11:29 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Assuming you have a nice class defined that will hold this data, and a nice setter that will set this attribute of objects of this nice class, then you'd simply add a check inside the setter that the parameter is actually positive. Throwing an exception if not.
Java Code:public class MyNiceClass { private double myAttribute; /** nice bit of javadoc in here which says that myAttribute cannot be negative. */ public void setMyAttribute(double myAttribute) { if (myAttribute < 0) throw new IllegalArgumentException("NEGATIVE!!"); // Or some other reasonable one this.myAttribute = myAttribute; } }
- 05-04-2011, 11:46 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
I'd extend the Number class and create an immutable non-negative double class; something like this:
kind regards,Java Code:class PosDouble extends Number { private double d; public PosDouble(double d) { if (d >= 0) this.d= d; else throw new IllegalArgumentException(d+" < 0"); } // the Number methods go here, and toString() etc. }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-04-2011, 12:01 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.
- 05-05-2011, 01:51 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
Instead of creating a class that throws exceptions, why not create a class that automatically changes it? Surely that's more useful?
I see how an exception is useful if the numbers are set and you need to know if they aren't negative, but if they simply 'have to be' negative set up an exception then add one of the given methods for converting it. That way you can make a method to check and possibly convert any number you want, and simply call it for each given number.
Yours stating the obvious,
~Karenthian~
- 05-05-2011, 08:07 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
The reason for throwing the exception is because the value is wrong.
Changing it (multiplying by -1 for example) is merely a case of guessing what the value should be. The plain fact is (and all you know is) that you have received an invalid value. Where it comes from is irrelevant.
If your double value should be non-negative then accepting a negative value is disguising another problem in your system...that being where this negative number (that shouldn't exist) is coming from.
So yes, it should be an exception.
- 05-05-2011, 09:07 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
True, and together with the fact that the class is immutable, throwing an unchecked exception at construction time guarantees that such an object will always be valid. Think of it, if d is negative, storing -d instead is just an arbitrary hack; it could've stored 42 just as well because that is also a positive value. But maybe people want to show their cleverness by showing the most tricky ways to make d a non-negative value.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
find the unique values of a double array
By tyang in forum New To JavaReplies: 3Last Post: 09-11-2011, 02:47 PM -
Negative,Positive and Zero
By dipowo in forum New To JavaReplies: 2Last Post: 04-04-2011, 05:49 AM -
Removing the double values?
By Lund01 in forum Advanced JavaReplies: 13Last Post: 11-17-2010, 11:34 AM -
Negative one for illegal/default values
By diablo84 in forum New To JavaReplies: 1Last Post: 08-08-2010, 07:58 PM -
mutliplicatio of double values
By katkamravi in forum New To JavaReplies: 2Last Post: 04-13-2009, 02:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks