473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Help with Equation in C++

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 1497
Banfa
9,065 Expert Mod 8TB
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
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...
8
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...
38
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...
5
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...
7
by: sonhadorPR | last post by:
How do I write the quadratic equation in C++? Thank You! SonhadorPR
4
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 -...
6
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...
2
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...
5
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.