Problematic Decimal Arithmetic in Javascript

by Dean

It is a pretty well known fact that using javascript to add decimals 0.1 with 0.2 does not result in 0.3. [1] Try it yourself with the FireBug console. For the uninitiated, the problem stems from javascript’s internal representation of numbers. They are actually binary numbers that are usually exact, but sometimes for example, are 0.00000000000000004 off. This is particularly aggravating when writing calculators that rely on js to give accurate results.

In my text inputs I was using toFixed() and some magic HTML attributes to keep decimals nice and clean. However, this method breaks down when a user enters a number with more significant figures than initially set up or you try to operate on two numbers with different sig figs. It was probably inevitable that I use a little arithmetic library extending Number to make decimals play nice.

Since javascript is 13 years old I thought it would be a simple thing to find such a library. I was wrong. After four days learning, looking and lamenting I had no library. After putting this one together in about a day and a half I am not surprised that nobody published theirs. Most of the eleven functions are one-liners, yet it bothers me that there are probably thirty-odd implementations of the this out there and not one found through Google.

  1. // decimal_arithmetic.js
  2. String.prototype.digitsAfterDecimal = function()
  3. {  var parts = this.split(".", 2);  // FIXME: Not international!
  4.    if( ! parts[1] )
  5.    {  parts[1] = "";  }
  6.    return parts[1].length;
  7. };
  8.  
  9. Number.prototype.biggerScalar = function(n)
  10. {  return n.scale() > this.scale() ? n.scale() : this.scale();  };
  11.  
  12. Number.prototype.digitsAfterDecimal = function()
  13. {  return this.toString().digitsAfterDecimal();  };
  14.  
  15. Number.prototype.divided = function(n)
  16. {  return this.dividedBy(n);  };
  17.  
  18. Number.prototype.dividedBy = function(n)
  19. {  return this.multiply( n.reciprocal() );  };
  20.  
  21. Number.prototype.minus = function(n)
  22. {  return this.plus( n.negative() );  };
  23.  
  24. Number.prototype.multiply = function(n)
  25. {  var s = this.biggerScalar(n);
  26.    return (Math.round(s*this,0) * Math.round(s*n,0)) / (s*s);
  27. };
  28.  
  29. Number.prototype.negative = function()
  30. {  return -1 * this;  };
  31.  
  32. Number.prototype.plus = function(n)
  33. {  var s = this.biggerScalar(n);
  34.    return (Math.round(s*this,0) + Math.round(s*n,0)) / s;
  35. };
  36.  
  37. Number.prototype.reciprocal = function()
  38. {  return 1 / this;  };
  39.  
  40. Number.prototype.scale = function()
  41. {  return Math.pow(10, this.digitsAfterDecimal() );  };
  42.  

Now you can do magical things like:

  1. 0.1.plus(0.2)
  2. // 0.3

yielding the correct results.

I am looking forward to javascript 2.0 when I can override the + operator. Maybe I won’t go that far since binary arithmetic is still faster.

[1] http://groups.google.com/group/comp.lang.javascript/…

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 10:09 pm

Categories: Dean, code, javascript |

2 Comments »

 Extending acts_as_commentable

by Dean

acts_as_commentable is a nice little ruby on rails plugin. It extends your ActiveRecord classes giving them comments. We are going to use comments on all kinds of things, starting with recipes, of course. However, AAC lacks a critical feature: the ability for users to approve comments before they are displayed. In this post I am going to run through extending AAC using acts_as_state_machine.

The first thing I did (and do to all the plugins we use) was pistonize the plugin so I could hack on it without fear of getting my changes destroyed.

I start off simply here by adding two states to the Comment model: :pending and :approved.

  1. class Comment < ActiveRecord::Base
  2.   # The first element of this array is the initial state
  3.   VALID_STATES = [ :pending, :approved ]
  4.   acts_as_state_machine :initial => VALID_STATES[0]
  5.  
  6.   event :approve do
  7.     transitions :from => :pending, :to => :approved
  8.   end
  9.  
  10.   VALID_STATES.each do |_state|
  11.     # Define _state as a state
  12.     state _state
  13.   end
  14.  
  15.   # More code snipped
  16. end

Now we are going to write some real code, so here comes a little RSpec. aac provides three class methods:

  1. class Comment < ActiveRecord::Base
  2.   class << self
  3.   # Helper class method to lookup all comments assigned
  4.   # to all commentable types for a given user.
  5.   def find_comments_by_user(user)
  6.  
  7.   # Helper class method to look up all comments for
  8.   # commentable class name and commentable id.
  9.   def find_comments_for_commentable(commentable_str, commentable_id)
  10.  
  11.   # Helper class method to look up a commentable object
  12.   # given the commentable class name and id
  13.   def find_commentable(commentable_str, commentable_id)
  14.   end

Since it didn’t come with Test::Unit or RSpec tests I wrote up some test for these methods.

  1. describe Comment, "class methods" do
  2.   fixtures :comments, :recipes, :users
  3.   it "should find comments by user" do
  4.     Comment.find_comments_by_user( comments(:comment_one).user ).should all_belong_to( comments(:comment_one).user )
  5.   end
  6.  
  7.   # This could be more specific
  8.   it "should find comments for a particular class" do
  9.     Comment.find_comments_for_commentable( Comments(:comment_one).commentable_type, comments(:comment_one).commentable_id ).should be_an_instance_of(Array)
  10.   end
  11.  
  12.   it "should find all comments for a particular class" do
  13.     # I happen to know that comment_one is a recipe comment
  14.     Comment.find_commentable( "Recipe", comments(:comment_one).commentable_id ).should be_an_instance_of(Recipe)
  15.   end
  16.  
  17. end

If you are confused by should all_belong_to then you should check out my previous post. With these specs out of the way we can go on to adding more new code.

  1.  it "should find approved comments by user" do
  2.     Comment.find_approved_comments_by_user( comments(:comment_one).user ).should all_be_in_state("approved")
  3.   end
  4.  
  5.   it "should find pending comments by user" do
  6.     Comment.find_pending_comments_by_user( comments(:comment_one).user ).should all_be_in_state("pending")
  7.   end
  8. end

Now, normally you would write one spec at a time, but I think I would bore my readers, so I combined these two. Also take note that I am using another custom RSpec matcher all_be_in_state(). It looks a lot like all_belong_to(), so I leave its implementation as an exercise to the reader (unless I can get another blog post out of it). To get these tests to pass I add a few lines of code:

  1.  VALID_STATES.each do |_state|
  2.     # Define _state as a state
  3.     state _state
  4.  
  5.     # Add Comment.find__comments methods
  6.     ( class << self; self; end ).instance_eval do
  7.       define_method "find_#{_state}_comments_by_user" do |_user|
  8.         find_in_state( :all, _state, :conditions => ["user_id = ?", _user.id], :order => "created_at DESC" )
  9.       end
  10.     end
  11.   end

I am not a method_missing kind of guy, and prefer the dynamic-method metaprogramming style. This lot of code defines class methods at runtime that find Comments in specific states. I am actually using whytheluckystiff’s metaid to hide some of the meta-junk, but I thought I should spell it out here for clarity.

Well, now we have a Comment class with two states and code to limit finds to cmments in a specific state. Right now, that is all I have. Here is the full code for the Comment class and the RSpec. You will see another custom RSpec matcher here, require_a().

  1. class Comment < ActiveRecord::Base
  2.  
  3.   # The first element of this array is the initial state
  4.   VALID_STATES = [ :pending, :approved ]
  5.  
  6.   acts_as_state_machine :initial => VALID_STATES[0]
  7.  
  8.   belongs_to :commentable, :polymorphic => true
  9.   belongs_to :user
  10.  
  11.   event :approve do
  12.     transitions :from => :pending, :to => :approved
  13.   end
  14.  
  15.   validates_associated :user
  16.   validates_presence_of :comment, :commentable_id, :commentable_type, :state,                           :user_id
  17.  
  18.   VALID_STATES.each do |_state|
  19.     # Define _state as a state
  20.     state _state
  21.  
  22.     # Add Comment.find_<state>_comments methods
  23.     meta_def "find_#{_state}_comments_by_user" do |_user|
  24.       find_in_state( :all, _state, :conditions => ["user_id = ?", _user.id],
  25.                      :order => "created_at DESC" )
  26.     end
  27.   end
  28.  
  29.   class < < self
  30.  
  31.     # Helper class method to look up a commentable object
  32.     # given the commentable class name and id
  33.     def find_commentable(commentable_str, commentable_id)
  34.       commentable_str.constantize.find(commentable_id)
  35.     end
  36.  
  37.     # This could be refactored into find_<state>_comments_by_user (somehow)
  38.     def find_comments_by_user(_user)
  39.       find( :all, :conditions => ["user_id = ?", _user.id],
  40.             :order => "created_at DESC" )
  41.     end
  42.  
  43.     # Helper class method to look up all comments for
  44.     # commentable class name and commentable id.
  45.     def find_comments_for_commentable(commentable_str, commentable_id)
  46.       find( :all,
  47.             :conditions => [ "commentable_type = ? and commentable_id = ?",
  48.                              commentable_str, commentable_id ],
  49.             :order => "created_at DESC" )
  50.     end
  51.  
  52.   end
  53.  
  54. end</state>
  1. require File.dirname(__FILE__) + ‘/../../../../spec/spec_helper’
  2.  
  3. module CommentSpecHelper
  4.  
  5. end
  6.  
  7. describe Comment do
  8.  
  9.   fixtures :comments
  10.  
  11.   include CommentSpecHelper
  12.  
  13.   before(:each) do
  14.     @comment = Comment.new
  15.   end
  16.  
  17.   it "should start out in pending state" do
  18.     @comment.state.should == "pending"
  19.   end
  20.  
  21.   it "sould transition to approved" do
  22.     @comment = comments(:pending_comment)
  23.     @comment.approve!
  24.     @comment.state.should == "approved"
  25.   end
  26.  
  27.   it "should require a comment" do
  28.     @comment.should require_a(:comment)
  29.   end
  30.  
  31.   it "should require a commentable_id" do
  32.     @comment.should require_a(:commentable_id)
  33.   end
  34.  
  35.   it "should require a commentable_type" do
  36.     @comment.should require_a(:commentable_type)
  37.   end
  38.  
  39.   it "should require a state" do
  40.     @comment.should require_a(:state)
  41.   end
  42.  
  43.   it "should require a user_id" do
  44.     @comment.should require_a(:user_id)
  45.   end
  46.  
  47. end
  48.  
  49. describe Comment, "class methods" do
  50.  
  51.   fixtures :comments, :recipes, :users
  52.  
  53.   it "should find all comments for a particular class" do
  54.     # I happen to know that comment_one is a recipe comment
  55.     Comment.find_commentable( "Recipe", comments(:comment_one).commentable_id ).should be_an_instance_of(Recipe)
  56.   end
  57.  
  58.   it "should find comments by user" do
  59.     Comment.find_comments_by_user( comments(:comment_one).user ).should all_belong_to( comments(:comment_one).user )
  60.   end
  61.  
  62.   it "should find approved comments by user" do
  63.     Comment.find_approved_comments_by_user( comments(:comment_one).user ).should all_be_in_state("approved")
  64.   end
  65.  
  66.   it "should find pending comments by user" do
  67.     Comment.find_pending_comments_by_user( comments(:comment_one).user ).should all_be_in_state("pending")
  68.   end
  69.  
  70.   # This could be more specific
  71.   it "should find comments for a particular class" do
  72.     Comment.find_comments_for_commentable( comments(:comment_one).commentable_type, comments(:comment_one).commentable_id ).should be_an_instance_of(Array)
  73.   end
  74.  
  75. end

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 8:48 pm

Categories: Dean, code, ruby |

No Comments »

 belong_to RSpec matcher

by Dean

I was extending acts_as_commentable and needed a good RSpec test to check the returned objects from its finder methods belonged to the correct user. For example, Comment.find_comments_by_user( :some_user ) should all belong_to :some_user. I’ll be darned if that doesn’t look like a RSpec description. Since there is no all_belong_to matcher, I wrote one.

  1. module ActiveRecordValidations
  2.   class BelongTo
  3.     def initialize(expected)
  4.       @expected = expected
  5.     end
  6.  
  7.     def matches?(args)
  8.       args.all? do |target|
  9.         @target = target
  10.         @target.send(@expected.class.to_s.downcase) == @expected
  11.       end
  12.     end
  13.  
  14.     def failure_message
  15.       "expected #{@target.inspect} to all belong to #{@expected}"
  16.     end
  17.  
  18.     def negative_failure_message
  19.       "expected #{@target.inspect} not to all belong to #{@expected}"
  20.     end
  21.   end
  22.  
  23.   def belong_to(expected)
  24.     BelongTo.new( [expected] )
  25.   end
  26.  
  27.   def all_belong_to(expected)
  28.     BelongTo.new( expected )
  29.   end
  30. end

The matches? method takes an array of objects and goes through them with all? checking that they have a belongs_to the expected thing. Using the example above, each comment object returned by Comment.find_comments_by_user would get tested if comment.user== @user.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 4:59 pm

Categories: Dean, code, ruby |

No Comments »

 Concise Signup & Signin Pages

by Dean

Login, Signup

We are presented with these quick forms all the time. While it is easy to create standard login and signup pages, Amazon.com has a good one:

Amazon Sigin Image

What makes it good?

First of all, the prompts are written in plain English. Amazon sells to a wide slice of the population, meaning that about 15% of their customers are probably not very technology-saavy. (-2?) Anything they can do to ease the operation helps their customers buy.

Secondly, when a user visits Amazon.com, there is only one link: “Your account” instead of separate login and signup links. Simple is generally better.

Also note the standard “forgot your password” link plus an additional “has your email address changed?” question. Both are useful to have close at hand.

Implementation

Rails 2.0 strongly encourages you to design RESTful applications. Login forms are associated with Session objects, while signup forms go with User objects (rather, Brewer objects in our case). A simple redirect in the SessionsController#create method takes care of pointing a user in the right direction.

  1.  
  2. class SessionsController < ApplicationController
  3.   def create
  4.     if params[:signin_action] == ‘new_user’
  5.       redirect_to new_brewer_path( :brewer => {:email => params[:email]} )
  6.     else
  7.       # Do sigin stuff
  8.     end
  9.   end
  10. end

Note that we pass params[:email] to the new_brewer_path so that field is automatically populated on the next page. If you are using the generated scaffold, you will have to change your BrewersController#new method to instantiate a new @brewer object:

  1.  
  2. class BrewersController < ApplicationController
  3.   def new
  4.     @brewer = Brewer.new(params[:brewer])
  5.   end
  6. end

Lastly, here is the extra test:

  1.  
  2. class SessionsControllerTest < Test::Unit::TestCase
  3.   def test_should_redirect_to_new_brewer_if_asked
  4.     an_email = "dean@brewsession.com"
  5.     post :create, :email => an_email, :signin_action => ‘new_user’
  6.     assert_redirected_to new_brewer_path(:brewer => {:email => an_email} )
  7.     assert_nil session[:brewer_id]
  8.   end
  9. end

In a later post I will talk about how to implement the change password action in a RESTful way.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 10:16 am

Categories: Dean, code, ruby |

No Comments »

 Dean Moves

by Dean

My wife got an offer to do her Forensic Entomology PhD at Texas A&M university. It is her dream job to teach and do Forensics work, and since I work from anywhere we packed up and moved to Bryan.

We arrived a week ago, signed a lease, dropped off the dogs and cats and headed to O’Bannon’s, a decent bar that we found during our October visit. It’s nice to be in a place where I can easily get Dogfish Head IPA, but this place does not have the beer diversity of the SF Bay Area. The change in cost of living is awesome though.

What does this mean for Brewsession?  Not a heck of a lot.  We have always been more of a virtual team that just happened to get together for beer a few times a month.  Further, since I have no friends here )-: I can devote more time to coding.  I will also do some friend-finding.  Danger: the municipal golf course is practically across the street.

Look for a resurgence of development work.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 11:58 am

Categories: Dean |

No Comments »

 Could be an Obstacle

by Dean

Hi Reader,

I am going to tell you the truth. I was not looking forward to writing the javascript necessary to get BrewSession going. For one thing I am a stickler for strict separation of behavior and content. Cascading Style Sheets are great because they keep the page content from getting lost in the necessary formatting to make a pretty-looking page - there is no equivalent for keeping javascript from obfuscating the stuff on your page.

I have a working prototype for keeping javascript off the page, but I feel like it is just one more thing I need to finish before I can get to the real programming. MochiKit is nice to program with, but laying the foundation for a cool sparkley web site was dragging on.

Enter the Google Web Toolkit. It “…is an open source Java software development framework that makes writing AJAX applications … easy for developers who don’t speak browser quirks as a second language.” Using swing-like widgets and panels, you can quickly put together a functioning app. I do not know swing or even much java, however that is not the obstacle to which I alluded in this post’s title.

I’ve been steaming out a mini-app for … let’s call them a client. It’s quite easy and I’m pleased with the toolkit. I can already see the pieces working together for BrewSession. Now round-about to the problem. It is not currently well-integrated with Rails.

Allow me to geek out on you. Rails is an MVC architecture with great M and C tools and decent V tools. The community believes in doing it yourself, leaning towards being elitist about it. I already have a great start to developing BrewSession into a full-fledged REST service thanks to Rails. I still love working with ruby and am going to keep rails on the server side. All I need to do is get BrewSession, written in GWT, to consume the REST service.

Searching the google group for the toolkit brings up a whole lotta nothing, so it looks like I have to do it myself. The way things are going I will someday refer to myself as a programmer rather than a sys admin.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 10:58 pm

Categories: Dean, code |

No Comments »

 Two Stray Thoughts

by Dean

Hi Reader,

Before I get to a serious post I want to share two things in the forefront of my mind.

If you like my writing I have a personal brewing-related blog at the home of my brewery.

Secondly, I discovered an enchanting bitter at Magnolia a few weeks ago. Sadly, San Francisco is an hour’s drive so I will have to settle for the fine cask ales at Trails downtown.

That is all.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 10:17 pm

Categories: Dean |

No Comments »

 Demos on the way

by Dean

Hi Reader,

It has been a little while since an update, but do not get the impression that we are slacking off here.  Greg and I will put up some calculators for you to break.  Keep an eye on this blog.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 10:17 pm

Categories: Dean, demo |

No Comments »

 Dumb software annoys me

by Dean

Hi Reader,

Has it ever occured to you that your brewing software should be smart enough to figure out what type of recipe you are composing? It bothers me that I have to change this for every recipe I make. Now, with a few simple rules BrewSession will determine if you are making an all grain or an extract with specialty grain (Ew/SG) or a whatever recipe. You will be able to override that guess, because well, you’re smarter than a computer, even if you are more lazy than a computer. ;-)

This opens up the door to another little feature I’m excited about. BrewSession will present you with smart options about what extraction process corresponds to the recipe’s type. If the recipe is all grain you will get a full mash profile sheet, but for a Ew/SG there will be a simplified Extraction Details sheet. When BrewSession sees an extract recipe nothing of the sort will appear to reduce clutter and confusion. All of these nice options are controlled by the recipe type setting which you can leave up to BrewSession, or change for yourself:

BrewSession thinks this recipe is All Grain
Recipe Type Selector
Never mind the ugly colors and font, we will fix that.

Each time you add a grain, extract or adjunct BrewSession will check if the new fermentable alters the recipe type and updates accordingly. That makes one less thing that you have to change, getting you to brew-day faster.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 12:15 am

Categories: Dean, features |

2 Comments »

 Brew-day timeline

by Dean

Hi reader,

When BrewSession goes live we should have an automated brew-day timeline generator! What does that mean?

Once you create a recipe there will be a link to the timeline which will analyze your recipe and try to intelligently list the steps you should take to brew it up. The boil is easiest and I have it mostly done:

  1. Bring the sweet wort to a boil.
  2. Add 1.5 ozs of 14.2% AA Horizon, boil for 45 minutes
  3. Add 2.0 ozs of 7.5% AA East Kent Goldings, boil for 5 minutes
  4. Add 1 tbs of Irish Moss boil for 10 minutes

See, wasn’t that easy? How about the post-boil?

  1. Chill the wort to 60-75° F and collect in your fermenter
  2. Add 3 gal sanitized water
  3. Aerate the wort
  4. Pitch Burton Ale Yeast into the fermenter
  5. Add 2.0 ozs of 4.8% AA East Kent Goldings to the fermenter

Oooh, dry hops. Print it out, take it to the brewery if you don’t have a ‘puter right there. Mashing/Extraction is a little tricky, but given a detailed recipe I think that BrewSession can be a big help on brew day.

–Dean

On a side note, I pretty much have BrewSession back to the feature-level it was at before the crash and data loss. Took me about two months of hard work in the spare time I’m not working my real job. A big “Thank you” to my understanding wife for not grumbling (much) about the time I spend in front of my keyboard.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 11:27 pm

Categories: Dean, features |

No Comments »

Our Sponsors