473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Equation in C++

3 New Member
I'm trying to have C++ solve an equation I have set up.
The equation is:

angle = (1/16)*(pow(LS,2) + LS*sqrt(pow(LS, 2) + pow(16,2) - 1) - 1);

where LS is a number from -1 to 1 by increasing it by 0.0001

I have angle and LS defined as double, when I run this program it tells me that the angle is equal to -1, -0, or 0 no matter what LS is.

I know that the angle should have decimal places, no idea why it keeps giving a number of 1 or 0 and not a decimal (like it should be doing)

I included the cmath and cstdlib in the program. I didn't attach the whole code, because its way too long, and I think I traced the problem down to this equation.
Oct 29 '07 #1
1 1517
Banfa
9,065 Recognized Expert Moderator Expert
I suspect your problem is in the (1/16) (I can't see anything else obvious). These are integers and in integer arithmatic this value is 0.

In an equation like this it is worth writting all constants as double costants rather than integer constants to ensure that you get the meaning you intend without any effects from doing integer arithmatic. This isn't hard just add .0 to all the constants e.g. (1.0/16.0).

also pow(16, 2) is a constant value, 256. You equation will be more efficient if you put in the constant value rather than calling the pow function.

And finally I am not sure that pow is a particularly efficient way of squaring, that is i Think pow(LS, 2) would be better as (LS*LS).
Oct 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
7087
by: matthiasjanes | last post by:
dear all, I just need a little help. could anyone give real code example (simple) how to read the value of a grid cell. i could not figure it out and really would like to do a simple spreadsheet application in wxpython (my first try with wxpython gui)
8
2010
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error msg that im getting: "Error executing cl.exe" this is the code that I have, but I know whats causing it, ill just show you the whole thing first though //file: Quadratic
38
2552
by: Red Dragon | last post by:
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery. Why does the computer refuse to execute my scanf ("%c",&q); On input 3 4 1 (for a,b and c) I had real roots OK On input 1 8 16 I had same real roots OK. However on 4 2 5, (for imaginary roots ) the computer cannot see the scanf ("%c",&q); statement. It just jumps...
5
4455
by: DBQueen | last post by:
I have a database where the user will have the opportunity to set up a number of Tests. I want the user to be able to enter the equation if a field for a particular test is to be a calculated value. This will be entered into tblTests. There are 4 data fields in tblSamples (Data1, Data2, Data3, Data4). When setting up a new test the user will fill in the equation for any caluclated data fields into fields Calc1, Calc2, Calc3, Calc4 in...
7
3626
by: sonhadorPR | last post by:
How do I write the quadratic equation in C++? Thank You! SonhadorPR
4
1855
by: SijSaru13 | last post by:
Dear everybody, I have to write a program that implements a real-time multicast calculator service. Users join a multicast group and are able to send equations to other clients for calculation - no separate server is needed. Calculations are sent in a single string to the multicast group and the result is returned once it has been calculated by a client in single string containing the equation and its result. The client must be able to...
6
14849
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a cannot equal 0 are given by the following formula -b + or - square root of (b^2 - 4ac) / 2a. If b^2 - 4ac = 0 then equation has a single root. if b^2 - 4ac > 0 then equation has two real roots. if b^2 - 4ac < 0 then equation has two complex...
2
2126
by: DaRok28 | last post by:
// Program Description: // This program solves quadratic equations to find their roots. This // program takes values of a, b, and c as input and outputs the root(s). // The user can repeat the calculation for as many equations as they like. #include <iostream> #include <cmath> #include <complex> using namespace std;
5
1275
by: mulakerakha | last post by:
I have to write a programme. Whic is like this: You are write a program whcih uses the quadratic equation to solve second-order polynominal. wchich is a x**2 + b x + c =0 and use the quadratic equation, when given value for the coefficient a, b and c, to find the value of x for which the equation is true the quadratic equation is -b +- sqrt (b**2 -4ac) X = -------------------------------------------- 2a
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.