File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes approach value to 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "approach value to 1" Watch "approach value to 1" New topic
Author

approach value to 1

Lukas Benberg
Ranch Hand

Joined: Jan 03, 2007
Posts: 45
I have a variable that represents a double value in the range between 0 and 1. This variable should increase (i.e. come closer to 1) everytime a certain event occurs (say, a user clicks).

How (mathematically) would I calculate the variable, so that it comes closer to 1 everytime the event happens? This is probably pretty easy, but I'm lost here...

The problem I have is that I don't have the overall number of events (n) that already occured, but only the current value (v) of the variable. Otherwise I could calculate v = 1-1*n.

Thanks for your hints!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

How (mathematically) would I calculate the variable, so that it comes closer to 1 everytime the event happens? This is probably pretty easy, but I'm lost here...


This is a weird requirement. You need to get closer to one, but you don't care how you approach it?

How about...

new_value = prev_value + ((1 - prev_value) / 1000);

Of course, the precision of a double isn't infinite. And it will eventually become one.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, you could add half the remaining interval every time an event occurs:

x += (1 - x)/2.0


[Jess in Action][AskingGoodQuestions]
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
Lukas Benberg wrote:
The problem I have is that I don't have the overall number of events (n) that already occured, but only the current value (v) of the variable. Otherwise I could calculate v = 1-1*n.


I don't think that'd return a value between 0 and 1..... What use would (1-n) be?
I mean, n being an integer....


Lukas Benberg
Ranch Hand

Joined: Jan 03, 2007
Posts: 45
Thank you all,
I think this is what I had in my mind: x += (1 - x)/2.0 .

Henry, for the moment I don't care _how_ I approach 1. Later, this may become relevant. I will have to think about your hint.

Prabz, you are right, what I actually meant was: v = 1-1/n

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11155
    
  16

i think however you do it, you will have a limit to how many clicks they can make. at some point, you will hit the limit on the precision of a float/long, and you're done.

can you perhaps better explain the larger problem? Maybe there is a better solution...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Lukas Benberg
Ranch Hand

Joined: Jan 03, 2007
Posts: 45
Hi Fred,
the context is: I want to calculate something like the user's "preferences" for "items" from data I have. for further analysis, all preference values have to be between 0 and 1 (or alternatively between -1 and 1). I don't know in advance the final "preference" value, because preferences may increase later. also, the number of "items" may change later.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: approach value to 1
 
Similar Threads
Calculate crc checksum md5
Adding to PAGE_NUMBER Variable
Data evaluates to translate paramerer
DDML
Ask Threads and Events..