SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Help with Math and JavaScript

    I think I'm too stupid to solve this math equation myself.
    Given:
    Code:
    0.05 * 0.05 = 0.0025
    How do I make the result having greater value like 0.25, like they multiply integral number.

    Was this possible at all and how do you calculate in JavaScript.

    I want the numbers lesser than zero multiply with each another to having greater value not lesser.

    Thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Sorry, I don't understand you.

    .05 times .05 is .0025

    So that result is correct. You want to purposely get an incorrect result?

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I must be very stupid at math.
    I want something like: 0.05 + 0.05 + 0.05 + 0.05 +0.05

    It must multiply with integral number. A kindergarten could do this.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    I don't know about that. When I was in kindergarten they didn't teach me decimals or fractions.

    So what you want is
    .05 times 5

    Code:
    5        5      25
    --    x  --  =  --
    100       1     100

    not
    .05 times .05

    Code:
    5          5         25
    --    x   --     =   --
    100       100      10000

  5. #5
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that's more likely.

    I calculate two time stamps. The first one is actually 0.0012208 seconds and the second is 0.0743354146 seconds.

    I want to multiply them. It's impossible to have lesser value.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    No programming language can multiply decimal fractions accurately because they all work with binary fractions.

    If you want an accurate answer you need to make them all integers first and only convert back to a fraction at the end.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Doing something like
    var result = ( (timestamp1 * 1000000000) * (timestamp2 * 1000000000) ) / 1000000000;

    might give a more accurate result without losing precision, but I have to wonder why would timestamps ever need to be multiplied?

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Doing something like
    var result = ( (timestamp1 * 1000000000) * (timestamp2 * 1000000000) ) / 1000000000;
    would give a wrong answer - you are multiplying by 1000000000000000000 but only dividing by 1000000000. The actual answer would be 1/1000000000 of what that calculation would produce.

    The problem with converting to integers so as to not lose precision is in keeping track of what to divide by at the end.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    :d'oh: right you are, thanks for catching that. The point I was focusing on (and obviously not giving much thought to the rest ) was why would one want to multiply timestamps?

  10. #10
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for help. I called it wrongly. It's speed not timestamps. Problem with all not native English speaker I guess.

  11. #11
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Addition: by speed, more multiple factors mean delay.

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    That makes more sense.

    I would do something like
    duration = end_timestamp - start_timestamp
    for each thing you're timing. That would give you units of actual time to work with which you could convert to milli or micro seconds and should be a lot better to use IMHO


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •