473,404 Members | 2,114 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,404 software developers and data experts.

Wrong answer by multiplying float and integer

#include <stdio.h>

int main()
{
printf("Hello, %d \n",(int)(64.07*100));

return 0;
}
Jun 10 '15 #1
2 22892
weaknessforcats
9,208 Expert Mod 8TB
The answer is correct.

64.07 * 100 is a double multiplied by an integer. Never a good idea to mix floating point with integers but in this case the 100 is converted to double and the result is double.

Now you have (int)(64.07 * 100 ) which tells the compiler to treat the double as an int. Even though the bit layout of the double is not the bit layout of an int. That cast does not convert the double to an int. All it does is tell the compiler to not be putting out warnings because you know more about what you are doing then the compiler does.

You can't really convert a float to an integer since the integer has no decimal portion making any conversion inaccurate.

Unless you have a really good reason for using floating point you should stick with integers.
Jun 10 '15 #2
I suppose the real is: "Why does 64.07 * 100 not simply equal 6407?"

Well, can 64.07 be represented in binary? Let's try
2^6 + 2^-4 + 2^-8...

To be honest, there's really not point in trying, as the answer most definitely is no.

Just like you can't represent 1/3, pi, sqrt(2), in decimal, you cannot represent 64.07 and many other numbers in binary.

And since integer casts are always floored, that is to say rounded down, you result's one less that you'd expect.

However you can get very very close, which is why if you remove the cast and change %d to %f, you will get the correct result.

I suppose if you really want to do it your way you'd need to round it.

Expand|Select|Wrap|Line Numbers
  1. printf( "Hello, %d \n", lround( 64.07 * 100 ) )
And voila! The correct result appears.

And just to make sure there's not confusions, '%d' does not mean double, but signed integer. You need to use '%f' to print floats and doubles.

Best regards.
Jun 16 '15 #3

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

Similar topics

11
by: Eugene Anthony | last post by:
The code bellow: function isInt(input) isInt = true for i=1 to len(input) d = Mid(input,i,1) if Asc(d) < 48 OR Asc(d) > 57 then isInt = false exit for
5
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
7
by: nephish | last post by:
Hullo all ! i have a real easy one here that isn't in my book. i have a int number that i want to divide by 100 and display to two decimal places. like this float(int(Var)/100) but i need it...
4
by: Geky | last post by:
Why when I multiply a float , for example X10, and than convert it as an integer the new value is changed? Example: float fl = 1.8 int x; x = int(fl * 10);
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
3
by: =?Utf-8?B?U2FzaQ==?= | last post by:
create a J# windows applicaion.add a button , a label and two textboxes to form and paste this code into the buttons event handler: private void button1_Click(Object sender, System.EventArgs e)...
8
by: .nLL | last post by:
<%=2/33334%> returns 5.99988000239995E-05 how can i convert it to a n.nn ?
4
by: HughLafferty | last post by:
#include <iostream> using namespace std; int main () { int numberOfStems=100; float lowMark=1/4; cout << "Low Mark= " << lowMark <<endl; //This outputs 0 cout << "Pass Mark=" <<...
8
by: depster | last post by:
#include<stdio.h> #include<string.h> int main() { char a; int i,j,b,x,temp,t=1024; while(t>0) { scanf("%s",a); if(strlen(a)==1 && a=='1')
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.