Solved

Imitating % for ints

Posted on 2004-09-13
8
243 Views
Last Modified: 2012-05-05
Hi,

I need to imitate the '%' operator that C offers.
But I also need it to work with floats.
The floats isn't the problem.
What I need it the code that imitates '%' (for negative number too, of course).

Thanks in advance.
0
Comment
Question by:slavikn
  • 4
  • 4
8 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 12043191
How far did you get yourself? Converting a number to a string isn't too difficult.
0
 
LVL 1

Author Comment

by:slavikn
ID: 12043207
strings? hmm....

while (a >= b)
   a -= b;

but this doesn't work if a and b are negative.
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 25 total points
ID: 12043260
Ahem :-$ sorry! I thought you meant the % in printf... Never mind...

a%b = a-(int)(a/b)*b

where \ is the integer divide operator. Try if it works for negative numbers.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:slavikn
ID: 12043509
> Try if it works for negative numbers.

Yes, it does. Thanks!
0
 
LVL 1

Author Comment

by:slavikn
ID: 12045120
heh   :-D
'fmod' from 'math.h' does exactly the same  :-))
'man pages' say it uses same formula  :-))
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 12045371
They didn't get it from me ;-)  Nor did I do a man fmod, but maybe I will...
0
 
LVL 1

Author Comment

by:slavikn
ID: 12045445
Heh ;-)   I wrote this to say two things:
1) your formula is correct.
2) I could use a ready-made function instead.
:-)))
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 12045663
Better to use fmod, since converting to int will not work for numbers that go beyond integer precision. Probably fmod is then defined as a-floor(a/b)*b. Or so.

Sjef :)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 119
Test against App 49 137
Problem to scan all sheets 3 103
Resolve Dependency Issues 4 64
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now