C++ :: Dividing Two Floats Not Giving The Required Quotient
Jan 9, 2014
Im using the remquo function in the cmath library as follows:
int quotient;
double a = remquo (10.3, 4.5, "ient);
This gives the correct remainder (a = 1.3) and quotient (quotient = 2).
Infact about 50% of the answers are right when I play around, however, trying something like:
int quotient;
double a = remquo (2.25, 1.5, "ient);
yields an incorrect quotient of 2 and remainder of 0.
I do think this has something to do with float arithmetic. I recall tinkering with the float number 0.500 and that the CPU actually saves it as 0.50000000000000231. However if my suspicion of float arithmetic as the suspect is correct, I do not understand why a tenth decimal would make such a drastic difference as changing the quotient result.
View 10 Replies
ADVERTISEMENT
May 14, 2014
i am relatively new to C programming so i run into problems on daily basis. But this time i have something i just cant figuer out and i was hoping you could point me towards the right track. I am trying to divide two integers.DevValue by KpTotal. for some reason my micro controller allways crashes.Y is a variable of a distance measuring sensor. i have a 4x4 keypad to enter a three digit number (e.i 123) so Kp1 = 1 Kp2 = 2 Kp3 =3.
Code:
int kp1, kp2, kp3, kpTotal = 0;
char txt[6] = ""
int keypadPort at PORTD;
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
}
[code]....
i think it has something to do with the format of the value. i read that the micro controller crash when dividing by zero.
View 1 Replies
View Related
Apr 4, 2015
I wrote the following code to divide 100 customers into three clusters but it kept on hanging during execution. I used while loop. Attached is the text file to use. and the code below :
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <math.h>
[Code] ....
Attached File(s)
c201.txt (7.13K)
View 1 Replies
View Related
Jun 21, 2013
i made a calculator that you have to give terms and operators one by one but now i want to improve it. the user now should be able to type something like 5*78+325/sin(3*pi)-1 and the program should be able to calculate it. but i don't know how to use the signs '*','+','-' and '/' as delimiters and turn the input string into a string array. i mean the string "5*78+325/sin(3*pi)-1" will become a string array like "5,*,78,+,325,/,sin(3*pi),-,1,"
View 2 Replies
View Related
Mar 2, 2013
So i have this -
time_t t4 = time(NULL);
//and then some stuff
time_t t5 = time(NULL);
iElapsedYah = t5-t4;
fElapsedYah2 = iElapsedYah/iNumTimes;
printf("It took %f seconds", fElapsedYah2);
but it always prints 0.0000000, and i dont know why even though individually
iElapsedYah = not zero and iNumTimes = not zero
I am using windows 7 and m complier is Dev C++
View 3 Replies
View Related
May 5, 2014
I need to read a float number and show the rest of his division by an integer, but i'm having the following error message:
Quote
error: invalid operands of types 'float' and 'int' to binary 'operator%'
View 8 Replies
View Related
Dec 2, 2014
So I have a simple calculator that does a few operations (+ - * / %) Pretty basic stuff
I declared int x, y for the numbers, char operation, and float result.
the code is based on switch(operation)
The program is running alright, but when I divide 8/7 it returns 1 as the result, I tried changing the x and y to float but that won't work because of the case '%'
I also tried making local float variables in the case'/' but it won't compile "E2126 Case bypasses initialization of a local variable"
How can I make the division work and return a float value?
View 3 Replies
View Related
Jan 30, 2013
I am having a hard time with some of my homework, specifically regarding how to printf floats. I can't seem to print the number i want out using float, it just becomes a jumbled mess.
Code:
#include <stdio.h>
#define TICKER "LRCX"
#define PURCHASE_DATE "01/02/13"
#define SELL_DATE "01/30/13"
#define INVESTMENT_AMOUNT "10,000.00"
[Code] .....
Thats the code I currently have, I've probably tried everything to get the number to come out, but I just cant seem to figure it out. It should look like this, but with different numbers and stock:
Stock: MCD Buy Date: 01/02/13 Sell Date: 01/29/13 Buy Share Price: $89.40 Sell Share Price: $91.50 Shares Purchased: 111.86
Amount of Investment: $10,000.00 Value of Shares Sold: $10,234.90 Amount of Gain/Loss: $234.90 Percent Gain/Loss: 2.35%
However, this is how mine turns out:
Code::Blocks
Enter share purchase price for LRCX=>23
Enter the selling price for LRCX=>23
Stock: LRCX
Buy Date: 01/02/13
Sell Date: 01/30/13
Buy Share Price: -1.#R
Sell Share Price: -1.#R
Shares Purchased: -1.#R
Amount of Investment: 10,000.00
Value of Shares Sold:-1.#R
Amount of Gain/Loss:-1.#R
Percent Gain/Loss:-1.#R%
Process returned 0 (0x0) execution time : 2.864 s
Press any key to continue.
View 3 Replies
View Related
Mar 12, 2014
I'm supposed to store the value of a countrys population. Then gather out the percentage that countrys population holds when compared with the global population.
Anyway here's the code:
Code:
#include <iostream>
long swe_pop = 9644864;
int main ()
[Code] .....
The result I'm getting is 0%.
I was under the impression that long (or long long) integers could hold high values. And that I could then divide these and answer with a float type value. Giving space for the decimals.
View 3 Replies
View Related
Sep 16, 2014
I am new with C programming and trying to learn how to create functions such as floats. But for some reason when I try to compile this program the compiler will tell me Weight() is not a function.
Code:
#include <stdio.h>
float Weight(float Mass, float g)
{
float Weight = 0;
g = 9.81;
[Code] ....
View 3 Replies
View Related
Dec 24, 2014
I'm trying to compare two float ranges and it seems to be a hit and miss. I'm trying to use a object origin and dimensions comparing it to another set for collisions.
View 12 Replies
View Related
Jun 27, 2013
I am very new at c++ and ran into this problem.
pairPercentage = ((float)tempPair/hands) * 100;
flushPercentage = ((float)tempFlush/hands) * 100;
totalPairPercentage = pairPercentage + totalPairPercentage;
is there something I should be awarded of when adding two float(s) ? because totalPairPercentage will always return pairPercentage, not its summation.
also did (totalPairPercentage += pairPercentage);
View 3 Replies
View Related
Nov 6, 2012
Can we use modulo to compare two floats?
View 3 Replies
View Related
Apr 24, 2012
Say I have a std::vector of size 10 of float. This will be a windowing average, as each cycle I need to remove the oldest element and add a new element. I figured vector was good because I can pop_front() then push_back(). Is there any clever way to get the average of the vector without using a for loop?
View 5 Replies
View Related
Mar 14, 2012
how do you convert integers into floats / I remember there is something like ftoi, I am looking into something handy like the std
View 6 Replies
View Related
Aug 7, 2013
I'm getting unexpected output in 2 different cases. The 1st deals with bitfields. The C++ standard has this line about integral promotions:
An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the
bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.
If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.
This sounds like the value of a bitfield will always be treated as a signed int if the signed representation of the value will fit in the bits. This seems to hold true for my C compiler, but not my C++ compiler.
I tried storing a small negative value in a bitfield that has enough bits to store the sign bit and the value. But when I print out the bitfield, I always get a large number
In the example code below, I expect the output:
Code:
foo.x = -1
foo.y = -2
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = 4294967295 But I get: Code: foo.x = 31
foo.y = 6
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = -1 -------------------
The other issue I'm having is sort of similar. I'm trying to store 4294967295 into a float, but when I print it out, I get 4294967296. i've tried storing a few other large values like this and what's printed out is rarely the value I stored. I thought it might be because of some int to float conversion, so I tried 4294967295.0. Still no luck. Then I remember that defaults to a double so maybe that's the issue so I tried 4294967295.0f. Still no luck. Why can't I store the correct value here? I don't think it's an IEE format thing since I can use these values as floats on a calculator program.
The example code showing both issues is below.
Code:
#include <stdio.h>
typedef struct {
signed char x : 5;
signed char y : 3;
}my_struct_t;
[Code] .....
View 11 Replies
View Related
Apr 11, 2014
I can't get the values correctly parsed.
Code:
float **get(int sizeX, int sizeY) /* Allocate the array */ {
float** ary = new float*[sizeX];
for(int i = 0; i < sizeX; ++i)
ary[i] = new float[sizeY];
return ary;
[Code] ....
test.txt (without the comments).
Code:
2 3 // columns and rows of 2D array
0 0
1 1
2 2
View 3 Replies
View Related
Oct 28, 2013
I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).
Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.
View 5 Replies
View Related
Apr 30, 2013
Code:
void search(){void output(void);
char title[20],;
char *p;
clrscr();
[Code] ......
Info:Program that stores information about reports .the above function searches the report according to its title. list is the name of the structure that stores the records.
Why i'm using strstr:
for eg. there is a report titled 'report on tigers'
I want the report information to be output if someone searches for 'tiger'
Output:displays all the entries i have made till now
file is attached.
View 4 Replies
View Related
Jan 8, 2015
Why is using namespace needed in linux but not in turbo c++?
View 1 Replies
View Related
Sep 5, 2014
I actually am wanting to practice on a 2 dimension char array. so, I am using the below program and getting the error - Lvalue Required For Function Main
#include <stdio.h>
#include <conio.h>
#include <string.h>
char varStringArray[1][5];
int main(void) {
clrscr();
varStringArray[0] = 'a';
printf("%c", varStringArray[0]);
return 0;
}
View 2 Replies
View Related
Jul 22, 2013
I have programmed a game where you guess a number (1-6) and if the number is equal to the random number then give the player score + 10. But if I have selected for example 4 players then if the game will give player 1 a score it gives player 2 a score instead? What can be causing this error?
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int cube;
int number[4];
[Code] .....
View 6 Replies
View Related
Sep 27, 2013
I want to give audio-input to a FFT code (KissFFT) written in C, on a real-time basis. While I can give a simple test signal (like sine wave) by writing the sine function as input, I am not sure how I should convert an audio-signal (e.g.: song) into a form that can be taken as input by the KissFFT C code.
View 3 Replies
View Related
Jul 20, 2013
The program should ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen.
#include <iostream>
#include <string>
using namespace std;
[Code].....
The problem I'm having is that where I declare movies = [numStudents]; the semicolon after the numStudents array is giving me this error - "error: expected a '{' introducing a lambda body".
View 2 Replies
View Related
Feb 5, 2014
I'm creating a bank system. So I know to make a deposit to the balance, which add ups what is the balance to i have add.
When i run the deposit function, its work well in some ways. If balance(text file) has the value 10, add i addSum 20, the balance will become 30, same as the text file will become 30. so its work well to add positive number.
double deposit(double balance){
double addSum = 0;
system("CLS");
cout<< "Welcome to deposit."<<endl;
cout<<"Enter a sum you wish to add to your account:";
[Code] .......
When I withdraw from 30 which is the balance, then i takeSum, for example i take away 30. The balance will become 30 - 30 = 0
When i make another withdraw from example -150, it will be -150.
Which shows correct.
But when i make a deposit from -150 and i addSum 130, the balance shows -500, and it should had been -20.
double withdraw(double balance) {
double takeSum = 0;
system("CLS");
cout<< "Welcome to withdraw."<<endl;
cout<<"Enter a sum you wish to take away from your account:";
cout << balance << '
[Code] .....
What is causing this problem, also when function deposit and withdraw close, it goes to readBalance function, should go to menu.
double readBalance(double balance) {
int option;
system("CLS");
cout<<"Welcome to balance."<<endl;
cout<<"Your balance is:"<<endl;
[Code] .....
View 1 Replies
View Related
Jul 30, 2014
Not a major issue since I got this to work but for some reason both my random numbers are the same and not sure why ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ComputerAssistedInstructions {
private static Random rand1 = new Random();
private static Random rand2 = new Random();
[Code] .....
View 2 Replies
View Related