I'm trying to have precision of 12 decimals in C. I don't know if there's an easier solution. But at least that code works. Now I'm just trying to save the result in a "long double" but "strtold()" is not working
char* multiply12Decimals(float n1, float n2)
{
long n1Digits;
sscanf(doubleToVoidPointerInNewMemoryLocation(n1*1000000), "%ld", &n1Digits);
printf("n1Digits: %ld\n", n1Digits);
long n2Digits;
sscanf(doubleToVoidPointerInNewMemoryLocation(n2*1000000), "%ld", &n2Digits);
printf("n2Digits: %ld\n", n2Digits);
long long mult = (long long) n1Digits*n2Digits;
printf("mult: %lld\n", mult);
char *charNum = malloc(30*sizeof(char));
sprintf (charNum, "0.%012lld\n", mult);
printf("result: %s\n", charNum);
return charNum;
}
printf("%.12lf",num); solves the problem.
Multiply two double and print it like this. No need to use long.
Related
I am using a 32-bit system. I wanted to cast void* to long long(64-bit). I have tried as below and I am not getting expected values.
printf("size of long long is %d and unsigned int is %d\n", sizeof(long long), sizeof(unsigned int));
void* ptr = 12340000;
long long test = (long long)ptr;
printf("the value of the ptr is %d\n", ptr);
printf("the value of the test is %d\n", test);
The output for the above code is:
size of long long is 8 and unsigned int is 4
the value of the ptr is 12340000
the value of the test is 1610658408
Let's start with getting the format strings match the types you pass.
#include <stdio.h>
int main(void)
{
printf("size of long long is %zu and unsigned int is %zu\n", sizeof(long long), sizeof(unsigned int));
void* ptr = (void *) 12340000;
long long test = (long long) ptr;
printf("the value of the ptr is %p\n", ptr);
printf("the value of the test is %lld\n", test);
}
If you want to check text in hexadecimal (hint: use %llx) you will see it is probably the same as ptr in a slightly different representation. You shouldn't cast a void * to long long without testing if that actually provides the correct results on your platform.
But be very careful with format strings. For sizeof you should use %zu, for void * it is %p and for long long it is %lld.
The cast to long long is just: (long long).
#include <stdio.h>
int main() {
unsigned long int sum = 0LL;
long i;
for (i = 0LL; i < 100000000; i++) {
sum = sum + i;
}
printf("%i", sum);
}
that's all of my code, and I am curious why it prints 887459712 instead of 4999999950000000, and how to fix this
Your sum is declared unsigned long int, which is big enough for your expected result.The problem is not overflow.
The problem is your printf statement is wrong.
You told it to print a "normal" sized int with %i, which only goes up to about 4.2 billion.
You should tell printf to print an unsigned long int, using %llu
It should be:
printf("%llu", sum);
IDEOne Link
Results
Success #stdin #stdout 0s 5376KB
4999999950000000
The nth term of the series is f(n).
f(n) = 1, when n=1
f(n)= ((n-1)*(8*(n–2)* 2+ 20)+4) , when n>1
P(n)=f(1)+f(2)+.....f(n)
1<=n<=10^9
for a given n we have to find P(n) modulo 10^9+7.
I solved the equation and finally getting the answer as
P(n)=(16n^3-18n^2+14n-12)/3
Problem comes when I implemented it in c++.
Given below the code tell me whats wrong with it and how to resolve it?
#include<stdio.h>
#define c 1000000007
int main()
{
long long int t,n,sum;
scanf("%lld",&t);
while(t--)
{
sum=0;
scanf("%lld",&n);
if(n==1)
printf("1\n");
else
{
sum=(((16*(((((n%c)*(n%c))%c)*(n%c))%c)%c)+14*(n%c))%c-(18*(((n%c)*(n%c))%c)%c+12)%c)/3;
sum++;
printf("%lld\n",sum);
}
}
return 0;
}
There are multiple issues in your code.
Problem 1:
An expression like long long b = ((a%c)*(a%c))%c; with a and b being long long
and c just a "plain number", ie. int, is still subject to int overflow, without
using the full capabilities of long long. The reason is the result of modulo:
long long = ((long long % int) * (long long % int)) % int;
long long = (int * int) % int;
long long = int % int; //overflow here
long long = int;
long long = long long; //expanding too late
//finished
As an example to check (http://ideone.com/kIyM7K):
#include <iostream>
using namespace std;
int main() {
int i = 1000000;
long long ll = 1000000;
cout<< ((999999%i)*(999999%i)*(999999%i)) << endl;
cout<< ((999999%ll)*(999999%ll)*(999999%ll)) << endl;
return 0;
}
In your code, 1000000007 is not a long long ...
Use 1000000007ULL to treat is as unsigned long long
Problem 2:
You´re using (or at least "used", before the edits) %lld in printf and scanf
for unsigned long long, which is %llu. %lld would be a signed long long
Problem 3:
P(n) purely mathematical and without modulo is positive for every natural number n>=1.
But P(0) is negative, and more important: The four parts of your equation modul´t
and the added/subtracted together could result in a negative number, which is congruent
to the positive expected result, but C doesn´t know that.
So, use signed instead of unsigned, and after the whole calculation, check
if the result is <0 and add c to it to make it >0
Problem 4:
A problem with parenthesis and operator precedence somewhere in your long formula.
A working example, but without the loops and input:
#include<stdio.h>
int main()
{
signed long long n, res, c;
n = 2456ULL;
c = 1000000007ULL;
signed long long part1 = (16*(((n%c)*(n%c)%c)*(n%c)%c))%c;
signed long long part2 = (18*((n%c)*(n%c)%c))%c;
signed long long part3 = (14*(n%c))%c;
res = ((part1 - part2 + part3 - 12)%c)/3 + 1;
printf("%lld\n", res);
return 0;
}
Not a problem:
You don´t need a special check for n=1, because the
whole calculation will result in 1 anyways.
I am having some problems with writing a C program for this question. Maybe am reading the question wrong and doing it the wrong way. Could someone help me with it please? This is they way I'm trying to do it
#include<stdio.h>
void main(void)
{
int j, sum=0;
long int product=1;
for(j=1;j<=30;j=j+2)
{
sum=sum+j;
}
for(j=2;j<=30;j=j+2)
{
product=product*j;
}
printf("\nThe sum of positive odd numbers is: %d", sum);
printf("\nThe product of positive even numbers is: %d", product);
}
The output I am getting is:
The sum of positive odd numbers is: 225
The product of positive even numbers is: -1409286144
I am getting the product part wrong. I have tried using unsigned long int, long long, unsigned long long. Nothing works.
Try using %ld instead of %d in your printf:
printf("\nThe product of positive even numbers is: %ld", product);
Since it's a long int and not an int.
If you use long long int, you'd want %lld. You might need the long long size, given that this is a very very large product. I don't know if your platform's long int is 32 or 64 bit, but you will certainly need a 64 bit number here.
The long long format string can vary depending on your exact platform and compiler, but mostly things have standardized on %lld nowadays. In particular, old Microsoft compilers sometimes used %I64d.
There are no issues as far as the sum of all odd numbers less than 30 is concerned as it's only 225.But the product of all even numbers (or odd numbers for that matter) less than 30 is an enormous number.For that you need a data type with larger capacity.In the following program I have simply used double instead of long int for product and I have used the %e format specifier to display the product in prinf() in a neat way, though you can use %f as well.
#include<stdio.h>
int main(void) //Return type of main() is "int",not "void" as you've used
{
int j, sum=0;
double product=1; //Change type of "product" to "double"
for(j=1;j<=30;j=j+2)
{
sum=sum+j;
}
for(j=2;j<=30;j=j+2)
{
product=product*j;
}
printf("The sum of positive odd numbers is: %d\n", sum);
printf("The product of positive even numbers is: %e",product); //Use %e
}
Output
The sum of positive odd numbers is: 225
The product of positive even numbers is: 4.284987e+16
calculate use unsinged int (32bit)
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef unsigned short UInt16;
typedef unsigned UInt32;
typedef struct _unums {
size_t size;
UInt16 *nums;//array
} UNums;
void UNums_init(UNums *num, UInt16 n){
num->nums = malloc(sizeof(UInt16));
num->nums[0] = n;
num->size = 1;
}
void UNums_mul(UNums *num, UInt16 n){
UInt16 carry = 0;
size_t i;
for(i=0;i<num->size;++i){
UInt32 wk = n;
wk = wk * num->nums[i] + carry;
num->nums[i] = wk % 10000;
carry = wk / 10000;
}
if(carry){
num->size += 1;
num->nums = realloc(num->nums, num->size * sizeof(UInt16));
num->nums[i] = carry;
}
}
void UNums_print(UNums *num){
size_t i = num->size;
int w = 0;
do{
--i;
printf("%0*hu", w, num->nums[i]);
if(!w) w = 4;
}while(i!=0);
}
void UNum_drop(UNums *num){
free(num->nums);
num->nums = NULL;
}
int main( void ){
UNums n;
UInt16 i;
assert(sizeof(UInt32) == 4);//32bit
assert(sizeof(UInt16) == 2);//16bit
UNums_init(&n, 1);
for(i=2;i<=30;i+=2)
UNums_mul(&n, i);
UNums_print(&n);//42849873690624000
UNum_drop(&n);
return 0;
}
why should a code like this should provide a so high result when I give it the number 4293974227 (or higher)
int main (int argc, char *argv[])
{
unsigned long long int i;
unsigned long long int z = atoi(argv[1]);
unsigned long long int tmp1 = z;
unsigned long long int *numbers = malloc (sizeof (unsigned long long int) * 1000);
for (i=0; tmp1<=tmp1+1000; i++, tmp1++) {
numbers[i] = tmp1;
printf("\n%llu - %llu", numbers[i], tmp1);
}
}
Result should start with the provided number but starts like this:
18446744073708558547 - 18446744073708558547
18446744073708558548 - 18446744073708558548
18446744073708558549 - 18446744073708558549
18446744073708558550 - 18446744073708558550
18446744073708558551 - 18446744073708558551
ecc...
What's this crap??
Thanks!
atoi() returns int. If you need larger numbers, try strtol(), strtoll(), or their relatives.
atoi() returns (int), and can't deal with (long long). Try atoll(), or failing that atol() (the former is preferred).
You are printing signed integers as unsigned.