Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:32:32+00:00 2026-05-24T21:32:32+00:00

The class below is supposed to represent a musical note. I want to be

  • 0

The class below is supposed to represent a musical note. I want to be able to store the length of the note (e.g. 1/2 note, 1/4 note, 3/8 note, etc.) using only integers. However, I also want to be able to store the length using a floating point number for the rare case that I deal with notes of irregular lengths.

class note{
    string tone;
    int length_numerator;
    int length_denominator;
public:
    set_length(int numerator, int denominator){
        length_numerator=numerator;
        length_denominator=denominator;
    }
    set_length(double d){
        length_numerator=d; // unfortunately truncates everything past decimal point
        length_denominator=1;
    }
}

The reason it is important for me to be able to use integers rather than doubles to store the length is that in my past experience with floating point numbers, sometimes the values are unexpectedly inaccurate. For example, a number that is supposed to be 16 occasionally gets mysteriously stored as 16.0000000001 or 15.99999999999 (usually after enduring some operations) with floating point, and this could cause problems when testing for equality (because 16!=15.99999999999).

Is it possible to convert a variable from int to double (the variable, not just its value)? If not, then what else can I do to be able to store the note’s length using either an integer or a double, depending on the what I need the type to be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:32:33+00:00Added an answer on May 24, 2026 at 9:32 pm

    I see several possible solutions: the first is just to use double. It’s
    true that extended computations may result in inaccurate results, but in
    this case, your divisors are normally powers of 2, which will give exact
    results (at least on all of the machines I’ve seen); you only risk
    running into problems when dividing by some unusual value (which is the
    case where you’ll have to use double anyway).

    You could also scale the results, e.g. representing the notes as
    multiples of, say 64th notes. This will mean that most values will be
    small integers, which are guaranteed exact in double (again, at least
    in the usual representations). A number that is supposed to be 16 does
    not get stored as 16.000000001 or 15.99999999 (but a number that is
    supposed to be .16 might get stored as .1600000001 or .1599999999).
    Before the appearance of long long, decimal arithmetic classes often
    used double as a 52 bit integral type, ensuring at each step that the
    actual value was exactly an integer. (Only division might cause a problem.)

    Or you could use some sort of class representing rational numbers.
    (Boost has one, for example, and I’m sure there are others.) This would
    allow any strange values (5th notes, anyone?) to remain exact; it could
    also be advantageous for human readable output, e.g. you could test the
    denominator, and then output something like “3 quarter notes”, or the
    like. Even something like “a 3/4 note” would be more readable to a
    musician than “a .75 note”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the class below, I am using a singleThreadScheduledExecutor. My question is, do I
In the class below, only the second constructor takes a ForumThread object. Otherwise, it
I tried to send an email using this class below, but no success, no
I am using the class pasted below to listen for the keypress event ctrl
I just want to know if I am supposed to extend every class with
I want to have something like this below: template <class T> struct Container{ public:
I have the code below: namespace WindowMover { using System.Windows.Forms; static class Logic {
Consider the class below that represents a Broker: public class Broker { public string
Can any one volunteer why the class below fails? ... src/model/user.rb:18: undefined method `set_schema'
My C# class MyClass (below) has members a, b, c, d, e, and f.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.